#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ii = pair<int, int>;
#define pb push_back
#define pp pop_back
#define ff first
#define ss second
#define int ll
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> indexed_set;
bool cmp(ii A, ii B) {
return A.ff + A.ss < B.ff + B.ss;
}
int K, N, A, B, Pr[100001];
priority_queue<int> L, R;
vector<ii> arr;
void solve(int V) {
int md = L.size() ? L.top() : 1e9 + 7;
if(V <= md) {
L.push(V); A += V;
}
else {
R.push(-V); B += V;
}
if(L.size() > R.size() + 1) {
V = L.top(); L.pop();
R.push(-V); A -= V; B += V;
}
else if(L.size() < R.size()) {
V = -R.top(); R.pop();
L.push(V); A += V; B -= V;
}
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> K >> N; int res = 0, curr = 0;
for(int l = 0; l < N; l++) {
char P, Q; int S, T;
cin >> P >> S >> Q >> T;
if(P == Q) {
curr += abs(T - S);
continue;
}
arr.pb({S, T});
}
sort(arr.begin(), arr.end(), cmp);
N = arr.size(); curr += N;
for(int l = 0; l < N; l++) {
solve(arr[l].ff);
solve(arr[l].ss);
Pr[l] = B - A;
}
res = Pr[N - 1];
if(K == 2) {
A = B = 0;
while(!L.empty()) L.pop();
while(!R.empty()) R.pop();
for(int l = N - 1; l >= 0; l--) {
res = min(res, B - A + Pr[l]);
solve(arr[l].ff);
solve(arr[l].ss);
}
}
cout << res + curr << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
2 ms |
360 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 |
340 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 |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
352 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
2 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 |
29 ms |
5312 KB |
Output is correct |
13 |
Correct |
62 ms |
6404 KB |
Output is correct |
14 |
Correct |
46 ms |
5132 KB |
Output is correct |
15 |
Correct |
38 ms |
3908 KB |
Output is correct |
16 |
Correct |
34 ms |
5732 KB |
Output is correct |
17 |
Correct |
42 ms |
6324 KB |
Output is correct |
18 |
Correct |
39 ms |
6288 KB |
Output is correct |
19 |
Correct |
48 ms |
6320 KB |
Output is correct |
20 |
Correct |
36 ms |
5892 KB |
Output is correct |
21 |
Correct |
52 ms |
6132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 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 |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
336 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 |
2 ms |
348 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 |
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 |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
216 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
220 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
220 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 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 |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
36 ms |
4728 KB |
Output is correct |
26 |
Correct |
63 ms |
5048 KB |
Output is correct |
27 |
Correct |
87 ms |
5772 KB |
Output is correct |
28 |
Correct |
93 ms |
6388 KB |
Output is correct |
29 |
Correct |
105 ms |
6644 KB |
Output is correct |
30 |
Correct |
55 ms |
3440 KB |
Output is correct |
31 |
Correct |
44 ms |
6048 KB |
Output is correct |
32 |
Correct |
66 ms |
6284 KB |
Output is correct |
33 |
Correct |
70 ms |
6300 KB |
Output is correct |
34 |
Correct |
87 ms |
6296 KB |
Output is correct |
35 |
Correct |
63 ms |
5812 KB |
Output is correct |
36 |
Correct |
70 ms |
6024 KB |
Output is correct |
37 |
Correct |
28 ms |
4740 KB |
Output is correct |