#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll v[100005],s1,s2;
int mij;
vector<pair<int,int>>poz;
priority_queue<int>pq;
priority_queue<int,vector<int>,greater<int>>pq2;
void update(int x)
{
if (x<mij)
pq.push(x),s1+=x;
else
pq2.push(x),s2+=x;
if (pq.size()+2<=pq2.size()){
pq.push(mij);
s1+=mij;
mij=pq2.top();
pq2.pop();
s2-=mij;
}
else
if (pq.size()>=pq2.size()+2){
pq2.push(mij);
s2+=mij;
mij=pq.top();
pq.pop();
s1-=mij;
}
}
int cmp(pair<int,int>a,pair<int,int>b)
{
return a.second+a.first<b.first+b.second;
}
int main()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
int k,n,i;
ll ans=0;
scanf("%d%d",&k,&n);
for(i=0;i<n;i++){
char ch1,ch2;
pair<int,int>x;
cin>>ch1>>x.first>>ch2>>x.second;
if (ch1!=ch2){
poz.push_back(x);
++ans;
}
else
ans+=abs(x.first-x.second);
}
sort(poz.begin(),poz.end(),cmp);
for(i=0;i<poz.size();i++){
update(poz[i].first);
update(poz[i].second);
v[i+1]=s2-(ll)pq2.size()*mij+((ll)pq.size()-1)*mij-s1;
}
ll ans2=v[poz.size()];
if (k==2){
pq=priority_queue<int>();
pq2=priority_queue<int,vector<int>,greater<int>>();
mij=s1=s2=0;
for(i=poz.size()-1;i>0;i--){
update(poz[i].first);
update(poz[i].second);
ans2=min(ans2,v[i]+s2-(ll)pq2.size()*mij+((ll)pq.size()-1)*mij-s1);
}
}
printf("%lld\n",ans+ans2);
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:54:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(i=0;i<poz.size();i++){
| ~^~~~~~~~~~~
bridge.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
41 | scanf("%d%d",&k,&n);
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
388 KB |
Output is correct |
5 |
Correct |
2 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
364 KB |
Output is correct |
9 |
Correct |
2 ms |
364 KB |
Output is correct |
10 |
Correct |
2 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
2 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
364 KB |
Output is correct |
12 |
Correct |
64 ms |
3936 KB |
Output is correct |
13 |
Correct |
128 ms |
5728 KB |
Output is correct |
14 |
Correct |
87 ms |
4192 KB |
Output is correct |
15 |
Correct |
74 ms |
3544 KB |
Output is correct |
16 |
Correct |
94 ms |
4832 KB |
Output is correct |
17 |
Correct |
123 ms |
5472 KB |
Output is correct |
18 |
Correct |
99 ms |
5216 KB |
Output is correct |
19 |
Correct |
125 ms |
5472 KB |
Output is correct |
20 |
Correct |
103 ms |
4988 KB |
Output is correct |
21 |
Correct |
116 ms |
5216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
2 ms |
364 KB |
Output is correct |
20 |
Correct |
2 ms |
364 KB |
Output is correct |
21 |
Correct |
2 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
364 KB |
Output is correct |
23 |
Correct |
2 ms |
364 KB |
Output is correct |
24 |
Correct |
2 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
2 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
2 ms |
364 KB |
Output is correct |
20 |
Correct |
2 ms |
364 KB |
Output is correct |
21 |
Correct |
2 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
364 KB |
Output is correct |
23 |
Correct |
2 ms |
364 KB |
Output is correct |
24 |
Correct |
2 ms |
364 KB |
Output is correct |
25 |
Correct |
69 ms |
4404 KB |
Output is correct |
26 |
Correct |
88 ms |
4376 KB |
Output is correct |
27 |
Correct |
132 ms |
5088 KB |
Output is correct |
28 |
Correct |
142 ms |
5888 KB |
Output is correct |
29 |
Correct |
141 ms |
5760 KB |
Output is correct |
30 |
Correct |
73 ms |
3164 KB |
Output is correct |
31 |
Correct |
105 ms |
4852 KB |
Output is correct |
32 |
Correct |
133 ms |
5504 KB |
Output is correct |
33 |
Correct |
102 ms |
5492 KB |
Output is correct |
34 |
Correct |
137 ms |
5504 KB |
Output is correct |
35 |
Correct |
108 ms |
5376 KB |
Output is correct |
36 |
Correct |
129 ms |
5248 KB |
Output is correct |
37 |
Correct |
68 ms |
3968 KB |
Output is correct |