#include<bits/stdc++.h>
using namespace std;
long long INF = 1e18;
const int maxn = 1e5+5;
vector<int> cp;
vector<pair<int, int>> vec;
bool cmp(pair<int, int> a, pair<int, int> b){
return a.first + a.second < b.first + b.second;
}
map<int, long long> mp[2];
long long pfx[maxn], sfx[maxn];
long long crr = -1, a, b;
void init(){
mp[0].clear();
mp[1].clear();
mp[0][-1] = 0;
crr = -1;
a = 0;
b = 0;
}
void upd_crr() {
while(a < 0){
crr = mp[0].upper_bound(crr)->first;
a += mp[0][crr];
b += mp[1][crr];
}
while(a > 0){
a -= mp[0][crr];
b -= mp[1][crr];
crr = prev(mp[0].lower_bound(crr))->first;
}
}
long long get_ans(){
long long re = crr*a + b;
auto it = mp[0].upper_bound(crr);
if(it != mp[0].end())re = min(re, it->first*(a+it->second) + mp[1][it->first] + b);
return re;
}
void add_new(int l, int r){
if(crr < l){
a--;
b += l;
}
if(crr >= r){
a++;
b -= r;
}
mp[0][l]++;
mp[0][r]++;
mp[1][l]-=l;
mp[1][r]-=r;
upd_crr();
}
int k, n;
int main(){
ios::sync_with_stdio(false);
cin >> k >> n;
long long ans = INF;
long long base = 0;
for(int i = 0; i < n; i++){
char t1, t2;
pair<int, int> p;
cin >> t1 >> p.first >> t2 >> p.second;
if(p.first > p.second)swap(p.first, p.second);
base += p.second - p.first;
if(t1 != t2) {
base++;
vec.push_back(p);
cp.push_back(p.first);
cp.push_back(p.second);
}
}
if(vec.empty()){
cout << base;
return 0;
}
sort(vec.begin(), vec.end(), cmp);
init();
for(int i = 0; i < vec.size(); i++){
add_new(vec[i].first, vec[i].second);
pfx[i] = get_ans();
}
if(k == 1){
cout << base + pfx[vec.size()-1]*2;
return 0;
}
init();
for(int i = vec.size()-1; i >= 0; i--){
add_new(vec[i].first, vec[i].second);
sfx[i] = get_ans();
if(i)ans = min(ans, pfx[i-1] + sfx[i]);
else ans = min(ans, sfx[i]);
}
cout << base + ans*2;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:91:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(int i = 0; i < vec.size(); i++){
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
612 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
688 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
768 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
592 KB |
Output is correct |
12 |
Correct |
22 ms |
3772 KB |
Output is correct |
13 |
Correct |
201 ms |
29884 KB |
Output is correct |
14 |
Correct |
67 ms |
4812 KB |
Output is correct |
15 |
Correct |
99 ms |
17800 KB |
Output is correct |
16 |
Correct |
24 ms |
4552 KB |
Output is correct |
17 |
Correct |
137 ms |
29880 KB |
Output is correct |
18 |
Correct |
102 ms |
29624 KB |
Output is correct |
19 |
Correct |
129 ms |
28860 KB |
Output is correct |
20 |
Correct |
29 ms |
4792 KB |
Output is correct |
21 |
Correct |
150 ms |
29940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
856 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
2 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
26 ms |
4036 KB |
Output is correct |
26 |
Correct |
45 ms |
3772 KB |
Output is correct |
27 |
Correct |
399 ms |
26048 KB |
Output is correct |
28 |
Correct |
493 ms |
28688 KB |
Output is correct |
29 |
Correct |
465 ms |
28532 KB |
Output is correct |
30 |
Correct |
193 ms |
15460 KB |
Output is correct |
31 |
Correct |
25 ms |
3776 KB |
Output is correct |
32 |
Correct |
252 ms |
28604 KB |
Output is correct |
33 |
Correct |
220 ms |
28748 KB |
Output is correct |
34 |
Correct |
220 ms |
27340 KB |
Output is correct |
35 |
Correct |
32 ms |
3776 KB |
Output is correct |
36 |
Correct |
265 ms |
28532 KB |
Output is correct |
37 |
Correct |
26 ms |
3784 KB |
Output is correct |