#include <bits/stdc++.h>
#define ST first
#define ND second
#define PB push_back
using namespace std;
using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;
const int nax = 100 * 1000 + 10;
const ll INF = 1e18;
int n, m, k;
int opn[nax], clo[nax];
vi all;
ll add = 0;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> k >> n;
for(int i = 0; i < n; ++i) {
char t1, t2;
int d1, d2;
cin >> t1 >> d1 >> t2 >> d2;
add += abs(d1 - d2);
if(t1 != t2) {
opn[m] = min(d1, d2);
clo[m] = max(d1, d2);
all.PB(d1);
all.PB(d2);
m++;
}
}
sort(all.begin(), all.end());
if(m == 0) {
cout << add << "\n";
return 0;
}
if(k == 1) {
sort(opn, opn + m);
sort(clo, clo + m);
int ptr = 0, ptr2 = 0;
ll Lsum = 0, Rsum = 0;
ll ans = INF;
for(int i = 0; i < m; ++i) Rsum += opn[i];
for(int x : all) {
while(ptr2 < m && clo[ptr2] < x) {
Lsum += clo[ptr2];
ptr2++;
}
while(ptr < m && opn[ptr] < x) {
Rsum -= opn[ptr];
ptr++;
}
ll d = (ll)x * ptr2 - Lsum - (ll)x * (m - ptr) + Rsum;
ans = min(ans, d);
}
cout << ans * 2 + add + m << "\n";
} else {
ll ans = INF;
for(int x : all) {
ll cur = 0;
priority_queue<pi>PQ;
vi nopn, nclo;
for(int i = 0; i < m; ++i) {
if(opn[i] <= x) {
if(clo[i] >= x) continue;
cur += abs(x - clo[i]);
} else {
PQ.push({-(clo[i] + opn[i] - x), opn[i]});
nopn.PB(opn[i]);
nclo.PB(clo[i]);
}
}
sort(nopn.begin(), nopn.end());
sort(nclo.begin(), nclo.end());
int ptr = 0, ptr2 = 0;
ll Lsum = 0, Rsum = 0;
int lft = 0;
for(int i = 0; i < (int)nopn.size(); ++i) Rsum += nopn[i];
for(int y : all) {
if(y < x) continue;
while(ptr2 < (int)nclo.size() && nclo[ptr2] < y) {
Lsum += nclo[ptr2];
ptr2++;
lft++;
}
while(ptr < (int)nopn.size() && nopn[ptr] < y) {
Rsum -= nopn[ptr];
ptr++;
}
while(!PQ.empty()) {
auto z = PQ.top();
if(y < -z.ST) break;
PQ.pop();
lft--;
cur += z.ND - x;
z.ST = -z.ST;
Lsum -= (z.ST - z.ND + x);
}
ll d = cur + Rsum - (ll)y * ((int)nopn.size() - ptr) + (ll)lft * y - Lsum;
ans = min(ans, d);
}
}
cout << ans * 2 + add + m << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 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 |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 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 |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
328 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
29 ms |
1916 KB |
Output is correct |
13 |
Correct |
64 ms |
1996 KB |
Output is correct |
14 |
Correct |
49 ms |
1924 KB |
Output is correct |
15 |
Correct |
38 ms |
1316 KB |
Output is correct |
16 |
Correct |
36 ms |
1932 KB |
Output is correct |
17 |
Correct |
40 ms |
1988 KB |
Output is correct |
18 |
Correct |
56 ms |
1940 KB |
Output is correct |
19 |
Correct |
61 ms |
1996 KB |
Output is correct |
20 |
Correct |
38 ms |
2048 KB |
Output is correct |
21 |
Correct |
58 ms |
1996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
3 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
204 KB |
Output is correct |
12 |
Correct |
3 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
204 KB |
Output is correct |
12 |
Correct |
2 ms |
204 KB |
Output is correct |
13 |
Correct |
39 ms |
332 KB |
Output is correct |
14 |
Correct |
128 ms |
380 KB |
Output is correct |
15 |
Correct |
140 ms |
376 KB |
Output is correct |
16 |
Correct |
4 ms |
332 KB |
Output is correct |
17 |
Correct |
32 ms |
332 KB |
Output is correct |
18 |
Correct |
21 ms |
344 KB |
Output is correct |
19 |
Correct |
25 ms |
376 KB |
Output is correct |
20 |
Correct |
105 ms |
464 KB |
Output is correct |
21 |
Correct |
68 ms |
332 KB |
Output is correct |
22 |
Correct |
175 ms |
376 KB |
Output is correct |
23 |
Correct |
68 ms |
340 KB |
Output is correct |
24 |
Correct |
141 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
36 ms |
332 KB |
Output is correct |
14 |
Correct |
109 ms |
336 KB |
Output is correct |
15 |
Correct |
154 ms |
372 KB |
Output is correct |
16 |
Correct |
4 ms |
332 KB |
Output is correct |
17 |
Correct |
32 ms |
332 KB |
Output is correct |
18 |
Correct |
19 ms |
352 KB |
Output is correct |
19 |
Correct |
20 ms |
396 KB |
Output is correct |
20 |
Correct |
118 ms |
380 KB |
Output is correct |
21 |
Correct |
71 ms |
332 KB |
Output is correct |
22 |
Correct |
179 ms |
372 KB |
Output is correct |
23 |
Correct |
63 ms |
332 KB |
Output is correct |
24 |
Correct |
145 ms |
372 KB |
Output is correct |
25 |
Execution timed out |
2098 ms |
3304 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |