#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
#include <cassert>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long ll;
int N,K,n;
struct pp{
ll x,y;
bool operator<(const pp& r)const{
return x+y<r.x+r.y;
}
}a[100010];
ll base;
ll L[100010],R[100010];
priority_queue<ll> lo;
priority_queue<ll,vector<ll>,greater<ll> > hi;
ll loSum,hiSum;
void init(){
while(!lo.empty())lo.pop();
while(!hi.empty())hi.pop();
loSum=hiSum=0;
}
void push(ll x){
if(lo.size()==hi.size())lo.push(x),loSum+=x;
else hi.push(x),hiSum+=x;
if(!lo.empty()&&!hi.empty()&&lo.top()>hi.top()){
ll a=lo.top(),b=hi.top();
lo.pop();hi.pop();
loSum-=a,hiSum-=b;
lo.push(b),hi.push(a);
loSum+=b,hiSum+=a;
}
}
ll query(ll i){
i++;
ll mid = lo.top();
return i*mid-loSum + hiSum-i*mid;
}
void f(ll d[]){
init();
for(ll i=0;i<n;i++){
push(a[i].x);
push(a[i].y);
d[i] = query(i);
}
}
int main(){
scanf("%d%d",&K,&N);
char t1,t2;
ll x,y;
for(int i=0;i<N;i++){
scanf(" %c %lld %c %lld",&t1,&x,&t2,&y);
if(t1!=t2){
a[n].x=min(x,y);
a[n++].y=max(x,y);
base++;
}else{
base+=abs(x-y);
}
}
if(n==0){
printf("%lld\n",base);
return 0;
}
sort(a,a+n);
if(K==1){
f(L);
printf("%lld\n",L[n-1]+base);
}else{
f(L);
reverse(a,a+n);
f(R);
ll ans = L[n-1];
for(int i=0;i<n-1;i++){
ans = min(ans,L[i]+R[n-2-i]);
}
printf("%lld\n",ans+base);
}
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:62:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&K,&N);
^
bridge.cpp:66:42: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf(" %c %lld %c %lld",&t1,&x,&t2,&y);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4300 KB |
Output is correct |
2 |
Correct |
0 ms |
4300 KB |
Output is correct |
3 |
Correct |
0 ms |
4300 KB |
Output is correct |
4 |
Correct |
0 ms |
4300 KB |
Output is correct |
5 |
Correct |
0 ms |
4300 KB |
Output is correct |
6 |
Correct |
0 ms |
4300 KB |
Output is correct |
7 |
Correct |
0 ms |
4300 KB |
Output is correct |
8 |
Correct |
0 ms |
4300 KB |
Output is correct |
9 |
Correct |
0 ms |
4300 KB |
Output is correct |
10 |
Correct |
0 ms |
4300 KB |
Output is correct |
11 |
Correct |
0 ms |
4300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4300 KB |
Output is correct |
2 |
Correct |
0 ms |
4300 KB |
Output is correct |
3 |
Correct |
0 ms |
4300 KB |
Output is correct |
4 |
Correct |
0 ms |
4300 KB |
Output is correct |
5 |
Correct |
0 ms |
4300 KB |
Output is correct |
6 |
Correct |
0 ms |
4300 KB |
Output is correct |
7 |
Correct |
0 ms |
4300 KB |
Output is correct |
8 |
Correct |
0 ms |
4300 KB |
Output is correct |
9 |
Correct |
0 ms |
4300 KB |
Output is correct |
10 |
Correct |
0 ms |
4300 KB |
Output is correct |
11 |
Correct |
0 ms |
4300 KB |
Output is correct |
12 |
Correct |
33 ms |
7032 KB |
Output is correct |
13 |
Correct |
73 ms |
7032 KB |
Output is correct |
14 |
Correct |
66 ms |
7032 KB |
Output is correct |
15 |
Correct |
56 ms |
5752 KB |
Output is correct |
16 |
Correct |
33 ms |
7032 KB |
Output is correct |
17 |
Correct |
66 ms |
7032 KB |
Output is correct |
18 |
Correct |
46 ms |
7032 KB |
Output is correct |
19 |
Correct |
69 ms |
7032 KB |
Output is correct |
20 |
Correct |
53 ms |
7032 KB |
Output is correct |
21 |
Correct |
76 ms |
7032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4300 KB |
Output is correct |
2 |
Correct |
0 ms |
4300 KB |
Output is correct |
3 |
Correct |
0 ms |
4300 KB |
Output is correct |
4 |
Correct |
0 ms |
4300 KB |
Output is correct |
5 |
Correct |
0 ms |
4300 KB |
Output is correct |
6 |
Correct |
0 ms |
4300 KB |
Output is correct |
7 |
Correct |
0 ms |
4300 KB |
Output is correct |
8 |
Correct |
0 ms |
4300 KB |
Output is correct |
9 |
Correct |
0 ms |
4300 KB |
Output is correct |
10 |
Correct |
0 ms |
4300 KB |
Output is correct |
11 |
Correct |
0 ms |
4300 KB |
Output is correct |
12 |
Correct |
0 ms |
4300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4300 KB |
Output is correct |
2 |
Correct |
0 ms |
4300 KB |
Output is correct |
3 |
Correct |
0 ms |
4300 KB |
Output is correct |
4 |
Correct |
0 ms |
4300 KB |
Output is correct |
5 |
Correct |
0 ms |
4300 KB |
Output is correct |
6 |
Correct |
0 ms |
4300 KB |
Output is correct |
7 |
Correct |
0 ms |
4300 KB |
Output is correct |
8 |
Correct |
0 ms |
4300 KB |
Output is correct |
9 |
Correct |
0 ms |
4300 KB |
Output is correct |
10 |
Correct |
0 ms |
4300 KB |
Output is correct |
11 |
Correct |
0 ms |
4300 KB |
Output is correct |
12 |
Correct |
0 ms |
4300 KB |
Output is correct |
13 |
Correct |
0 ms |
4300 KB |
Output is correct |
14 |
Correct |
0 ms |
4300 KB |
Output is correct |
15 |
Correct |
0 ms |
4300 KB |
Output is correct |
16 |
Correct |
0 ms |
4300 KB |
Output is correct |
17 |
Correct |
0 ms |
4300 KB |
Output is correct |
18 |
Correct |
0 ms |
4300 KB |
Output is correct |
19 |
Correct |
0 ms |
4300 KB |
Output is correct |
20 |
Correct |
0 ms |
4300 KB |
Output is correct |
21 |
Correct |
0 ms |
4300 KB |
Output is correct |
22 |
Correct |
0 ms |
4300 KB |
Output is correct |
23 |
Correct |
0 ms |
4300 KB |
Output is correct |
24 |
Correct |
0 ms |
4300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4300 KB |
Output is correct |
2 |
Correct |
0 ms |
4300 KB |
Output is correct |
3 |
Correct |
0 ms |
4300 KB |
Output is correct |
4 |
Correct |
0 ms |
4300 KB |
Output is correct |
5 |
Correct |
0 ms |
4300 KB |
Output is correct |
6 |
Correct |
0 ms |
4300 KB |
Output is correct |
7 |
Correct |
0 ms |
4300 KB |
Output is correct |
8 |
Correct |
0 ms |
4300 KB |
Output is correct |
9 |
Correct |
0 ms |
4300 KB |
Output is correct |
10 |
Correct |
0 ms |
4300 KB |
Output is correct |
11 |
Correct |
0 ms |
4300 KB |
Output is correct |
12 |
Correct |
0 ms |
4300 KB |
Output is correct |
13 |
Correct |
0 ms |
4300 KB |
Output is correct |
14 |
Correct |
0 ms |
4300 KB |
Output is correct |
15 |
Correct |
0 ms |
4300 KB |
Output is correct |
16 |
Correct |
0 ms |
4300 KB |
Output is correct |
17 |
Correct |
0 ms |
4300 KB |
Output is correct |
18 |
Correct |
0 ms |
4300 KB |
Output is correct |
19 |
Correct |
0 ms |
4300 KB |
Output is correct |
20 |
Correct |
0 ms |
4300 KB |
Output is correct |
21 |
Correct |
0 ms |
4300 KB |
Output is correct |
22 |
Correct |
0 ms |
4300 KB |
Output is correct |
23 |
Correct |
0 ms |
4300 KB |
Output is correct |
24 |
Correct |
0 ms |
4300 KB |
Output is correct |
25 |
Correct |
46 ms |
7032 KB |
Output is correct |
26 |
Correct |
86 ms |
7032 KB |
Output is correct |
27 |
Correct |
123 ms |
7032 KB |
Output is correct |
28 |
Correct |
116 ms |
7032 KB |
Output is correct |
29 |
Correct |
126 ms |
7032 KB |
Output is correct |
30 |
Correct |
59 ms |
5752 KB |
Output is correct |
31 |
Correct |
43 ms |
7032 KB |
Output is correct |
32 |
Correct |
103 ms |
7032 KB |
Output is correct |
33 |
Correct |
66 ms |
7032 KB |
Output is correct |
34 |
Correct |
123 ms |
7032 KB |
Output is correct |
35 |
Correct |
96 ms |
7032 KB |
Output is correct |
36 |
Correct |
116 ms |
7032 KB |
Output is correct |
37 |
Correct |
33 ms |
7032 KB |
Output is correct |