#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define fi first
#define se second
const int mxN=1e5;
int k, n, med;
ll ba, a1[mxN+1], a2, bs, ts;
vector<pii> ps;
priority_queue<int> bpq;
priority_queue<int, vector<int>, greater<int>> tpq;
inline void upd(int x) {
if(x<med)
bpq.push(x), bs+=x;
else
tpq.push(x), ts+=x;
if(bpq.size()+2<=tpq.size()) {
bpq.push(med), bs+=med;
med=tpq.top(), tpq.pop(), ts-=med;
} else if(bpq.size()>=tpq.size()+2) {
tpq.push(med), ts+=med;
med=bpq.top(), bpq.pop(), bs-=med;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> k >> n;
for(int i=0; i<n; ++i) {
char z1, z2;
pii pi;
cin >> z1 >> pi.fi >> z2 >> pi.se;
if(z1!=z2) {
ps.push_back(pi);
++ba;
} else
ba+=abs(pi.fi-pi.se);
}
sort(ps.begin(), ps.end(), [](const pii &a, const pii &b) {
return a.fi+a.se<b.fi+b.se;
});
for(int i=0; i<ps.size(); ++i) {
upd(ps[i].fi), upd(ps[i].se);
a1[i+1]=ts-(ll)tpq.size()*med+((ll)bpq.size()-1)*med-bs;
}
a2=a1[ps.size()];
if(k==2) {
bpq=priority_queue<int>(), tpq=priority_queue<int, vector<int>, greater<int>>();
med=bs=ts=0;
for(int i=ps.size()-1; i>0; --i) {
upd(ps[i].fi), upd(ps[i].se);
a2=min(a1[i]+ts-(ll)tpq.size()*med+((ll)bpq.size()-1)*med-bs, a2);
}
}
cout << ba+a2;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:48:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<ps.size(); ++i) {
~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
484 KB |
Output is correct |
3 |
Correct |
2 ms |
564 KB |
Output is correct |
4 |
Correct |
3 ms |
564 KB |
Output is correct |
5 |
Correct |
3 ms |
564 KB |
Output is correct |
6 |
Correct |
2 ms |
564 KB |
Output is correct |
7 |
Correct |
2 ms |
564 KB |
Output is correct |
8 |
Correct |
4 ms |
564 KB |
Output is correct |
9 |
Correct |
3 ms |
668 KB |
Output is correct |
10 |
Correct |
2 ms |
668 KB |
Output is correct |
11 |
Correct |
4 ms |
668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
668 KB |
Output is correct |
2 |
Correct |
2 ms |
668 KB |
Output is correct |
3 |
Correct |
2 ms |
668 KB |
Output is correct |
4 |
Correct |
3 ms |
668 KB |
Output is correct |
5 |
Correct |
3 ms |
668 KB |
Output is correct |
6 |
Correct |
4 ms |
668 KB |
Output is correct |
7 |
Correct |
2 ms |
668 KB |
Output is correct |
8 |
Correct |
2 ms |
668 KB |
Output is correct |
9 |
Correct |
3 ms |
668 KB |
Output is correct |
10 |
Correct |
3 ms |
668 KB |
Output is correct |
11 |
Correct |
3 ms |
668 KB |
Output is correct |
12 |
Correct |
54 ms |
3444 KB |
Output is correct |
13 |
Correct |
94 ms |
3700 KB |
Output is correct |
14 |
Correct |
47 ms |
3700 KB |
Output is correct |
15 |
Correct |
34 ms |
3700 KB |
Output is correct |
16 |
Correct |
38 ms |
3700 KB |
Output is correct |
17 |
Correct |
56 ms |
3700 KB |
Output is correct |
18 |
Correct |
69 ms |
3700 KB |
Output is correct |
19 |
Correct |
73 ms |
3700 KB |
Output is correct |
20 |
Correct |
50 ms |
3700 KB |
Output is correct |
21 |
Correct |
63 ms |
3700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3700 KB |
Output is correct |
2 |
Correct |
3 ms |
3700 KB |
Output is correct |
3 |
Correct |
2 ms |
3700 KB |
Output is correct |
4 |
Correct |
2 ms |
3700 KB |
Output is correct |
5 |
Correct |
2 ms |
3700 KB |
Output is correct |
6 |
Correct |
2 ms |
3700 KB |
Output is correct |
7 |
Correct |
2 ms |
3700 KB |
Output is correct |
8 |
Correct |
2 ms |
3700 KB |
Output is correct |
9 |
Correct |
2 ms |
3700 KB |
Output is correct |
10 |
Correct |
2 ms |
3700 KB |
Output is correct |
11 |
Correct |
2 ms |
3700 KB |
Output is correct |
12 |
Correct |
2 ms |
3700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3700 KB |
Output is correct |
2 |
Correct |
2 ms |
3700 KB |
Output is correct |
3 |
Correct |
2 ms |
3700 KB |
Output is correct |
4 |
Correct |
2 ms |
3700 KB |
Output is correct |
5 |
Correct |
3 ms |
3700 KB |
Output is correct |
6 |
Correct |
2 ms |
3700 KB |
Output is correct |
7 |
Correct |
2 ms |
3700 KB |
Output is correct |
8 |
Correct |
2 ms |
3700 KB |
Output is correct |
9 |
Correct |
2 ms |
3700 KB |
Output is correct |
10 |
Correct |
2 ms |
3700 KB |
Output is correct |
11 |
Correct |
2 ms |
3700 KB |
Output is correct |
12 |
Correct |
2 ms |
3700 KB |
Output is correct |
13 |
Correct |
2 ms |
3700 KB |
Output is correct |
14 |
Correct |
3 ms |
3700 KB |
Output is correct |
15 |
Correct |
3 ms |
3700 KB |
Output is correct |
16 |
Correct |
2 ms |
3700 KB |
Output is correct |
17 |
Correct |
2 ms |
3700 KB |
Output is correct |
18 |
Correct |
2 ms |
3700 KB |
Output is correct |
19 |
Correct |
2 ms |
3700 KB |
Output is correct |
20 |
Correct |
2 ms |
3700 KB |
Output is correct |
21 |
Correct |
2 ms |
3700 KB |
Output is correct |
22 |
Correct |
3 ms |
3700 KB |
Output is correct |
23 |
Correct |
2 ms |
3700 KB |
Output is correct |
24 |
Correct |
4 ms |
3700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3700 KB |
Output is correct |
2 |
Correct |
2 ms |
3700 KB |
Output is correct |
3 |
Correct |
2 ms |
3700 KB |
Output is correct |
4 |
Correct |
3 ms |
3700 KB |
Output is correct |
5 |
Correct |
2 ms |
3700 KB |
Output is correct |
6 |
Correct |
2 ms |
3700 KB |
Output is correct |
7 |
Correct |
2 ms |
3700 KB |
Output is correct |
8 |
Correct |
2 ms |
3700 KB |
Output is correct |
9 |
Correct |
2 ms |
3700 KB |
Output is correct |
10 |
Correct |
2 ms |
3700 KB |
Output is correct |
11 |
Correct |
2 ms |
3700 KB |
Output is correct |
12 |
Correct |
2 ms |
3700 KB |
Output is correct |
13 |
Correct |
3 ms |
3700 KB |
Output is correct |
14 |
Correct |
2 ms |
3700 KB |
Output is correct |
15 |
Correct |
3 ms |
3700 KB |
Output is correct |
16 |
Correct |
2 ms |
3700 KB |
Output is correct |
17 |
Correct |
3 ms |
3700 KB |
Output is correct |
18 |
Correct |
3 ms |
3700 KB |
Output is correct |
19 |
Correct |
3 ms |
3700 KB |
Output is correct |
20 |
Correct |
2 ms |
3700 KB |
Output is correct |
21 |
Correct |
3 ms |
3700 KB |
Output is correct |
22 |
Correct |
3 ms |
3700 KB |
Output is correct |
23 |
Correct |
3 ms |
3700 KB |
Output is correct |
24 |
Correct |
2 ms |
3700 KB |
Output is correct |
25 |
Correct |
37 ms |
3752 KB |
Output is correct |
26 |
Correct |
51 ms |
3752 KB |
Output is correct |
27 |
Correct |
64 ms |
3752 KB |
Output is correct |
28 |
Correct |
85 ms |
3904 KB |
Output is correct |
29 |
Correct |
80 ms |
3904 KB |
Output is correct |
30 |
Correct |
36 ms |
3904 KB |
Output is correct |
31 |
Correct |
48 ms |
3904 KB |
Output is correct |
32 |
Correct |
61 ms |
3904 KB |
Output is correct |
33 |
Correct |
43 ms |
3904 KB |
Output is correct |
34 |
Correct |
77 ms |
3904 KB |
Output is correct |
35 |
Correct |
55 ms |
3904 KB |
Output is correct |
36 |
Correct |
81 ms |
3904 KB |
Output is correct |
37 |
Correct |
35 ms |
3904 KB |
Output is correct |