Лекция 0 (20.10.2022)
Лекция 1
‣
ll t;
cin >> t;
while(t--){
ll n;
cin >> n;
vector<ll> a(n);
for(ll i = 0; i<n; i++){
cin >> a[i];
}
ll ans = 0;
ll p = a[0];
for(ll j = 0; j < n - 1; j++){
if(a[j] < a[j + 1]){
ans += a[j + 1] - a[j];
}else{
ans += a[j] - a[j + 1];
p -= a[j] - a[j + 1];
}
}
ans += abs(p);
cout << ans << "\n";
}
‣
struct Point {
ll x, y;
};
bool comp(Point &A, Point &B){
// if ((A.x * A.x + A.y * A.y) <
// (B.x * B.x + B.y * B.y)){
// return 1;
// } else {
// return 0;
// }
return ((A.x * A.x + A.y * A.y) <
(B.x * B.x + B.y * B.y));
}
int main(){
ll n;
cin >> n;
vector<Point> a(n);
for(ll i = 0; i<n; i++){
ll x, y;
cin >> x >> y;
a[i].x = x;
a[i].y = y;
}
sort(a.begin(), a.end(), comp);
for(ll i = 0; i<n; i++){
cout << a[i].x << ' ' << a[i].y << "\n";
}
}
‣
ll x, n;
cin >> x >> n;
vector<ll> a(n);
for(ll i = 0; i<n; i++){
cin >> a[i];
}
ll l = 0, r = n;
while(r - l > 1){
ll mid = (l + r) / 2;
if(x < a[mid]){
r = mid;
} else {
l = mid;
}
}
Используем стандартные методы и итераторы:
auto it = lower_bound(a.begin(), a.end(), x);
cout << *it << "\n";
//или
ll it = lower_bound(a.begin(), a.end(), x) - a.begin();
cout << it << ' ' << a[it] << "\n";
//upper_bound() - аналогично, но возвращает строго больший элемент