#pragma GCC optimize("O3")
#pragma GCC target("popcnt")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define Waimai ios::sync_with_stdio(false),cin.tie(0)
#define FOR(x,a,b) for(int x=a,I=b;x<=I;x++)
#define pb emplace_back
#define F first
#define S second
const int SIZE = 1e5 + 5;
int n, k;
int a[2 * SIZE];
ll ans, sum[SIZE];
pair<int, int> p[SIZE];
ll lsum, rsum;
int lcnt, rcnt, mid;
priority_queue<int> lpq;
priority_queue<int, vector<int>, greater<int>> rpq;
void Add (int x) {
if (lpq.empty() || x <= lpq.top()) {
lcnt++;
lsum += x;
lpq.push (x);
} else {
rcnt++;
rsum += x;
rpq.push (x);
}
while (rpq.size() > lpq.size()) {
int y = rpq.top();
rpq.pop();
rcnt--;
rsum -= y;
lpq.push (y);
lcnt++;
lsum += y;
}
while (lpq.size() > rpq.size() + 1) {
int y = lpq.top();
lpq.pop();
lcnt--;
lsum -= y;
rpq.push (y);
rcnt++;
rsum += y;
}
mid = lpq.top();
}
void solve() {
cin >> k >> n;
if (k == 1) {
int siz = 0;
FOR (i, 1, n) {
char cx, cy;
int x, y;
cin >> cx >> x >> cy >> y;
if (cx == cy) ans += abs (x - y);
else {
ans++;
a[++siz] = x;
a[++siz] = y;
}
}
n = siz;
nth_element (a + 1, a + (n + 1) / 2, a + n + 1);
mid = a[(n + 1) / 2];
FOR (i, 1, n) ans += abs (a[i] - mid);
cout << ans << '\n';
return;
}
int siz = 0;
FOR (i, 1, n) {
char cx, cy;
int x, y;
cin >> cx >> x >> cy >> y;
if (cx == cy) ans += abs (x - y);
else {
ans++;
p[++siz] = {x, y};
}
}
n = siz;
sort (p + 1, p + n + 1, [] (auto p1, auto p2) {
return p1.F + p1.S < p2.F + p2.S;
});
FOR (i, 1, n) {
auto [x, y] = p[i];
Add (x), Add (y);
sum[i] += (lcnt - rcnt) * mid - lsum + rsum;
}
lcnt = rcnt = 0;
lsum = rsum = 0;
lpq = {}, rpq = {};
for (int i = n; i >= 1; i--) {
auto [x, y] = p[i];
Add (x), Add (y);
sum[i - 1] += (lcnt - rcnt) * mid - lsum + rsum;
}
cout << ans + *min_element (sum, sum + n + 1) << '\n';
}
int main() {
Waimai;
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
20 ms |
1872 KB |
Output is correct |
13 |
Correct |
35 ms |
3416 KB |
Output is correct |
14 |
Correct |
20 ms |
2228 KB |
Output is correct |
15 |
Correct |
16 ms |
2132 KB |
Output is correct |
16 |
Correct |
26 ms |
2664 KB |
Output is correct |
17 |
Correct |
23 ms |
3364 KB |
Output is correct |
18 |
Correct |
25 ms |
3020 KB |
Output is correct |
19 |
Correct |
26 ms |
3412 KB |
Output is correct |
20 |
Correct |
24 ms |
2928 KB |
Output is correct |
21 |
Correct |
25 ms |
3052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
224 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
35 ms |
3908 KB |
Output is correct |
26 |
Correct |
48 ms |
3992 KB |
Output is correct |
27 |
Correct |
63 ms |
4880 KB |
Output is correct |
28 |
Correct |
66 ms |
5792 KB |
Output is correct |
29 |
Correct |
68 ms |
5452 KB |
Output is correct |
30 |
Correct |
35 ms |
3144 KB |
Output is correct |
31 |
Correct |
50 ms |
5056 KB |
Output is correct |
32 |
Correct |
62 ms |
5460 KB |
Output is correct |
33 |
Correct |
57 ms |
5072 KB |
Output is correct |
34 |
Correct |
53 ms |
5428 KB |
Output is correct |
35 |
Correct |
38 ms |
5016 KB |
Output is correct |
36 |
Correct |
53 ms |
5176 KB |
Output is correct |
37 |
Correct |
27 ms |
3844 KB |
Output is correct |