#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+2;
const int inf=1e18+2;
multiset<pair<int,int> > lis;
pair<int,int> seg[N];
long long val_pre[N],ans=0,val_suf[N],sum,sum1,val;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int num,i,j,k,l,n,m=0,cnt=0,min1=inf;
cin>>num>>n;
char x,y;
for(i=1;i<=n;i++){
cin>>x>>j>>y>>k;
if(j>k){
swap(j,k);
}
if(x!=y){
ans++;
m++;
seg[m].first=j;
seg[m].second=k;
}
else{
ans+=k-j;
}
}
sort(seg+1,seg+1+m,[&](pair<int,int> x,pair<int,int> y){
return x.first+x.second<y.first+y.second;
});
lis.insert({-inf,0});
multiset<pair<int,int> >::iterator ite=lis.begin();
sum=0;
sum1=0;
cnt=0;
val=-inf;
for(i=m;i>0;i--){
sum+=seg[i].first;
sum+=seg[i].second;
//cout<<seg[i].first<<' '<<seg[i].second<<endl;
if(seg[i].first<val){
cnt++;
sum1+=seg[i].first;
}
if(seg[i].second<val){
cnt++;
sum1+=seg[i].second;
}
lis.insert({seg[i].first,m-i+1});
lis.insert({seg[i].second,m-i+1});
while(cnt<m-i+1){
ite++;
cnt++;
sum1+=ite->first;
}
while(cnt>m-i+1){
sum1-=ite->first;
ite--;
cnt--;
}
val_suf[i]=sum-2*sum1;
//cout<<i<<' '<<val_suf[i]<<endl;
val=ite->first;
}
if(num==1){
cout<<val_suf[1]+ans;
return 0;
}
lis.clear();
sum=0;
sum1=0;
cnt=0;
val=-inf;
lis.insert({-inf,0});
ite=lis.begin();
for(i=1;i<=m;i++){
sum+=seg[i].first;
sum+=seg[i].second;
if(seg[i].first<val){
cnt++;
sum1+=seg[i].first;
}
if(seg[i].second<val){
cnt++;
sum1+=seg[i].second;
}
lis.insert({seg[i].first,i});
lis.insert({seg[i].second,i});
while(cnt<i){
ite++;
cnt++;
sum1+=ite->first;
}
while(cnt>i){
sum1-=ite->first;
ite--;
cnt--;
}
val_pre[i]=sum-2*sum1;
//cout<<i<<' '<<val_pre[i]<<endl;
val=ite->first;
}
assert(val_pre[m]==val_suf[1]);
for(i=0;i<=m;i++){
//cout<<val_pre[i]<<' '<<val_suf[i+1]<<endl;
min1=min(min1,val_pre[i]+val_suf[i+1]);
}
cout<<ans+min1;
}
/*
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:12:16: warning: unused variable 'l' [-Wunused-variable]
int num,i,j,k,l,n,m=0,cnt=0,min1=inf;
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
760 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
552 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
476 KB |
Output is correct |
6 |
Correct |
3 ms |
592 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
3 ms |
508 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
504 KB |
Output is correct |
12 |
Correct |
118 ms |
15352 KB |
Output is correct |
13 |
Correct |
185 ms |
15352 KB |
Output is correct |
14 |
Correct |
138 ms |
13872 KB |
Output is correct |
15 |
Correct |
92 ms |
9280 KB |
Output is correct |
16 |
Correct |
106 ms |
15352 KB |
Output is correct |
17 |
Correct |
114 ms |
15352 KB |
Output is correct |
18 |
Correct |
82 ms |
15392 KB |
Output is correct |
19 |
Correct |
112 ms |
15352 KB |
Output is correct |
20 |
Correct |
118 ms |
15352 KB |
Output is correct |
21 |
Correct |
118 ms |
15324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
12 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
504 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
3 ms |
504 KB |
Output is correct |
18 |
Correct |
0 ms |
376 KB |
Output is correct |
19 |
Correct |
3 ms |
504 KB |
Output is correct |
20 |
Correct |
13 ms |
504 KB |
Output is correct |
21 |
Correct |
3 ms |
504 KB |
Output is correct |
22 |
Correct |
8 ms |
560 KB |
Output is correct |
23 |
Correct |
3 ms |
504 KB |
Output is correct |
24 |
Correct |
3 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
6 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
508 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
3 ms |
504 KB |
Output is correct |
18 |
Correct |
1 ms |
376 KB |
Output is correct |
19 |
Correct |
3 ms |
552 KB |
Output is correct |
20 |
Correct |
3 ms |
504 KB |
Output is correct |
21 |
Correct |
1 ms |
504 KB |
Output is correct |
22 |
Correct |
3 ms |
508 KB |
Output is correct |
23 |
Correct |
3 ms |
504 KB |
Output is correct |
24 |
Correct |
3 ms |
504 KB |
Output is correct |
25 |
Correct |
169 ms |
16816 KB |
Output is correct |
26 |
Correct |
228 ms |
16896 KB |
Output is correct |
27 |
Correct |
319 ms |
17780 KB |
Output is correct |
28 |
Correct |
359 ms |
18368 KB |
Output is correct |
29 |
Correct |
305 ms |
18296 KB |
Output is correct |
30 |
Correct |
127 ms |
9848 KB |
Output is correct |
31 |
Correct |
173 ms |
17656 KB |
Output is correct |
32 |
Correct |
182 ms |
18276 KB |
Output is correct |
33 |
Correct |
128 ms |
18008 KB |
Output is correct |
34 |
Correct |
181 ms |
18296 KB |
Output is correct |
35 |
Correct |
173 ms |
17876 KB |
Output is correct |
36 |
Correct |
199 ms |
18168 KB |
Output is correct |
37 |
Correct |
160 ms |
16760 KB |
Output is correct |