#include <bits/stdc++.h>
using namespace std;
int n, k;
long long sameside=0;
vector<pair<int, int>>diff;
bool comp(pair<int, int>a, pair<int, int>b){
return (a.first+a.second<b.first+b.second);
}
long long pre[100005];
multiset<int>low;
multiset<int>up;
long long lowsum=0;
long long upsum=0;
long long res=0;
void add(int x){
if (low.size()==0){
low.insert(x);
lowsum+=x;
return;
}
if (x<=*(--low.end())){
low.insert(x);
lowsum+=x;
}else{
up.insert(x);
upsum+=x;
}
}
long long ans=2e18;
void rebalance(){
while (up.size()>low.size()){
low.insert(*(up.begin()));
lowsum+=*(up.begin());
upsum-=*(up.begin());
up.erase(up.begin());
}
while (low.size()>up.size()+1){
up.insert(*(--low.end()));
upsum+=*(--low.end());
lowsum-=*(--low.end());
low.erase(--low.end());
}
}
void INSERT(int val){
add(val);
rebalance();
}
int main(){
cin>>k>>n;
for (int i=1;i <=n; i++){
char a, b;
int x, y;
cin>>a>>x>>b>>y;
if (a==b){
sameside+=abs(x-y);
}else{
diff.push_back({x, y});
}
}
// diff={{1, 1}, {2, 2}, {3, 4}, {5, 8}};
int n_=diff.size();
sameside+=n_;
sort(diff.begin(), diff.end(), comp);
for (int i=0; i<diff.size(); i++){
INSERT(diff[i].first);
INSERT(diff[i].second);
pre[i]=*(--low.end())*((int)low.size())-lowsum+upsum-(*(--low.end())*((int)up.size()));
//cout<<i<<" "<<pre[i]<<'\n';
}
ans=pre[n_-1];
if (k==2){
low.clear();
up.clear();
lowsum=upsum=0;
for (int i=n_-1; i>=0; i--){
INSERT(diff[i].first);
INSERT(diff[i].second);
ans=min(ans, *(--low.end())*((int)low.size())-lowsum+upsum-(*(--low.end())*((int)up.size())) + pre[i-1]);
}
cout<<ans+sameside;
}else
cout<<pre[n_-1]+sameside;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:68:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for (int i=0; i<diff.size(); i++){
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
2 ms |
328 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
428 KB |
Output is correct |
8 |
Correct |
2 ms |
328 KB |
Output is correct |
9 |
Correct |
2 ms |
452 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 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 |
316 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
324 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
456 KB |
Output is correct |
10 |
Correct |
2 ms |
336 KB |
Output is correct |
11 |
Correct |
3 ms |
344 KB |
Output is correct |
12 |
Correct |
105 ms |
11964 KB |
Output is correct |
13 |
Correct |
258 ms |
13572 KB |
Output is correct |
14 |
Correct |
145 ms |
11392 KB |
Output is correct |
15 |
Correct |
107 ms |
8104 KB |
Output is correct |
16 |
Correct |
167 ms |
12828 KB |
Output is correct |
17 |
Correct |
146 ms |
13652 KB |
Output is correct |
18 |
Correct |
135 ms |
13160 KB |
Output is correct |
19 |
Correct |
184 ms |
13480 KB |
Output is correct |
20 |
Correct |
138 ms |
13136 KB |
Output is correct |
21 |
Correct |
145 ms |
13348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
316 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 |
308 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 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 |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
312 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
2 ms |
316 KB |
Output is correct |
14 |
Correct |
2 ms |
320 KB |
Output is correct |
15 |
Correct |
2 ms |
352 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
324 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
312 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 |
316 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 |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
332 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 |
340 KB |
Output is correct |
20 |
Correct |
3 ms |
456 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
344 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
170 ms |
12052 KB |
Output is correct |
26 |
Correct |
241 ms |
12212 KB |
Output is correct |
27 |
Correct |
338 ms |
12940 KB |
Output is correct |
28 |
Correct |
344 ms |
13564 KB |
Output is correct |
29 |
Correct |
347 ms |
13568 KB |
Output is correct |
30 |
Correct |
165 ms |
7324 KB |
Output is correct |
31 |
Correct |
182 ms |
12892 KB |
Output is correct |
32 |
Correct |
207 ms |
13588 KB |
Output is correct |
33 |
Correct |
179 ms |
13088 KB |
Output is correct |
34 |
Correct |
228 ms |
13576 KB |
Output is correct |
35 |
Correct |
192 ms |
13180 KB |
Output is correct |
36 |
Correct |
218 ms |
13224 KB |
Output is correct |
37 |
Correct |
184 ms |
11932 KB |
Output is correct |