#include <bits/stdc++.h>
using namespace std;
#define ll long long
int k, n;
vector<pair<int, int> > p;
ll ans = 0ll;
ll solvek1(){
if(p.empty()) return 0ll;
ll res = 0ll;
vector<int> cr;
for(int i = 0; i < p.size(); i ++){
cr.push_back(p[i].first);
cr.push_back(p[i].second);
res -= (ll)(p[i].first + p[i].second);
}
sort(cr.begin(), cr.end());
for(int i = 2 * (int)p.size() - 1; i >= (int)p.size(); i --)
res += 2ll * cr[i];
return res + (int)p.size();
}
bool cmp(pair<int, int> a, pair<int, int> b){
return a.first + a.second < b.first + b.second;
}
vector<ll> comp(){
priority_queue<int> mx;
priority_queue<int, vector<int>, greater<int> > mn;
ll sum_mx = (ll)p[0].first, sum_mn = (ll)p[0].second;
mx.push((int)sum_mx); mn.push((int)sum_mn);
vector<ll> tmp; tmp.push_back(sum_mn - sum_mx);
for(int i = 1; i < p.size(); i ++){
int mxt = mx.top(), mnt = mn.top();
int lf = p[i].first, rg = p[i].second;
if(lf <= mxt && rg <= mxt){
mx.pop();
mx.push(lf); mx.push(rg);
mn.push(mxt);
sum_mx += lf + rg - mxt;
sum_mn += mxt;
}else if(lf >= mnt && rg >= mnt){
mn.pop();
mn.push(lf); mn.push(rg);
mx.push(mnt);
sum_mn += lf + rg - mnt;
sum_mx += mnt;
}else{
mx.push(lf); mn.push(rg);
sum_mx += lf; sum_mn += rg;
}
tmp.push_back(sum_mn - sum_mx);
}
return tmp;
}
ll solvek2(){
if(p.empty()) return 0ll;
sort(p.begin(), p.end(), cmp);
vector<ll> asc = comp();
reverse(p.begin(), p.end());
vector<ll> dec = comp();
ll res = 1e18;
for(int i = 0; i < (int)p.size() - 1; i ++)
res = min(res, asc[i] + dec[(int)p.size() - i - 2]);
return min(res + (ll)p.size(), solvek1());
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> k >> n;
for(int i = 0; i < n; i ++){
string tp1, tp2; int p1, p2;
cin >> tp1 >> p1 >> tp2 >> p2;
if(tp1 == tp2)
ans += (ll)abs(p1 - p2);
else
p.push_back({min(p1, p2), max(p1, p2)});
}
cout << ans + (k == 1 ? solvek1() : solvek2()) << endl;
return 0;
}
Compilation message
bridge.cpp: In function 'long long int solvek1()':
bridge.cpp:14:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | for(int i = 0; i < p.size(); i ++){
| ~~^~~~~~~~~~
bridge.cpp: In function 'std::vector<long long int> comp()':
bridge.cpp:39:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i = 1; i < p.size(); i ++){
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
324 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 |
37 ms |
3400 KB |
Output is correct |
13 |
Correct |
52 ms |
4860 KB |
Output is correct |
14 |
Correct |
37 ms |
3656 KB |
Output is correct |
15 |
Correct |
31 ms |
2844 KB |
Output is correct |
16 |
Correct |
51 ms |
4312 KB |
Output is correct |
17 |
Correct |
56 ms |
4856 KB |
Output is correct |
18 |
Correct |
44 ms |
4544 KB |
Output is correct |
19 |
Correct |
48 ms |
4868 KB |
Output is correct |
20 |
Correct |
36 ms |
4460 KB |
Output is correct |
21 |
Correct |
54 ms |
4684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 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 |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 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 |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
332 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 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 |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 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 |
324 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 |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
57 ms |
5444 KB |
Output is correct |
26 |
Correct |
60 ms |
5504 KB |
Output is correct |
27 |
Correct |
81 ms |
6244 KB |
Output is correct |
28 |
Correct |
95 ms |
6896 KB |
Output is correct |
29 |
Correct |
94 ms |
6936 KB |
Output is correct |
30 |
Correct |
49 ms |
3700 KB |
Output is correct |
31 |
Correct |
51 ms |
6200 KB |
Output is correct |
32 |
Correct |
78 ms |
6924 KB |
Output is correct |
33 |
Correct |
64 ms |
6500 KB |
Output is correct |
34 |
Correct |
85 ms |
6872 KB |
Output is correct |
35 |
Correct |
55 ms |
6484 KB |
Output is correct |
36 |
Correct |
72 ms |
6592 KB |
Output is correct |
37 |
Correct |
40 ms |
5424 KB |
Output is correct |