#include <bits/stdc++.h>
#define int long long
#define sz(x) (long long)(x.size())
using namespace std;
struct traj{
int l, r;
long double mid;
};
vector <traj> a;
int score(int mid) {
int ans(0);
vector<int> b;
int i(0);
for (;i < sz(a) && a[i].mid <= mid ; i++){
b.push_back(a[i].l);
b.push_back(a[i].r);
}
sort (b.begin(), b.end());
int b1 = b[sz(b) / 2];
b.clear();
int b2 = 0;
if (i != sz(a)) {
for (;i < sz(a); i++) {
b.push_back(a[i].l);
b.push_back(a[i].r);
}
sort(b.begin(), b.end());
b2 = b[sz(b) / 2];
}
i = 0;
for (;i < sz(a) && a[i].mid <= mid; i++) {
ans += abs(a[i].l - b1);
ans += abs(a[i].r - b1);
}
for (;i < sz(a); i++) {
ans += abs(a[i].l - b2);
ans += abs(a[i].r - b2);
}
return ans;
}
int trinary_search(int l, int r){
if (r - l <= 10) {
int ans(LLONG_MAX);
for (int i(l); i <= r; i++)
ans = min(ans, score(a[i].mid));
return ans;
}
int mid1 = a[l + (r - l) / 3].mid;
int mid2 = a[l + 2 * (r - l) / 3].mid;
if (score(mid1) <= score(mid2))
return trinary_search(l, l + 2 * (r - l) / 3);
return trinary_search(l + (r - l) / 3, r);
}
signed main(){
int K, N; cin >> K >> N;
int ans(0);
for (int i(0); i < N; i++) {
char riv1, riv2;
int pos1, pos2;
cin >> riv1 >> pos1 >> riv2 >> pos2;
if (pos2 < pos1) swap(pos1, pos2);
if (riv1 != riv2) {
ans++;
a.push_back ({pos1, pos2, (long double)( (pos1 + pos2) / 2)});
}
else ans += pos2 - pos1;
}
if (sz(a) == 0){
cout << ans << '\n';
return 0;
}
sort (a.begin(), a.end(), [](traj &A, traj &B){return A.mid < B.mid;});
if (K == 1) cout << ans + score(LLONG_MAX) << '\n';
else cout << ans + trinary_search(0, sz(a) - 1) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
520 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
448 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
48 ms |
7904 KB |
Output is correct |
13 |
Correct |
96 ms |
9652 KB |
Output is correct |
14 |
Correct |
61 ms |
7864 KB |
Output is correct |
15 |
Correct |
54 ms |
5568 KB |
Output is correct |
16 |
Correct |
61 ms |
8632 KB |
Output is correct |
17 |
Correct |
75 ms |
10172 KB |
Output is correct |
18 |
Correct |
77 ms |
9292 KB |
Output is correct |
19 |
Correct |
83 ms |
9332 KB |
Output is correct |
20 |
Correct |
72 ms |
8984 KB |
Output is correct |
21 |
Correct |
76 ms |
9144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
432 KB |
Output is correct |
8 |
Correct |
0 ms |
432 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Incorrect |
1 ms |
436 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
436 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |