#include<stdio.h>
#include<algorithm>
#include<queue>
#define abs2(a) ((a)<0?-(a):(a))
#define MX 100010
using namespace std;
typedef long long lld;
struct routes {
lld s, e;
bool operator< (const routes& c) const {
return s+e < c.s+c.e;
}
}arr[MX];
struct hset {
priority_queue<lld> mxheap, miheap;
lld mxsum, misum;
void clear(){
mxsum=misum=0;
while(!mxheap.empty())mxheap.pop();
while(!miheap.empty())miheap.pop();
}
void ins(lld a, lld b){
if(mxheap.empty()){
mxheap.push(a), mxsum+=a;
miheap.push(-b), misum+=b;
return;
}
lld mx=mxheap.top();
lld mi=-miheap.top(); // mx < mi
if(b <= mx){
mxheap.pop(), mxsum-=mx;
mxheap.push(a), mxsum+=a;
mxheap.push(b), mxsum+=b;
miheap.push(-mx), misum+=mx;
}
else if(a >= mi){
miheap.pop(), misum-=mi;
miheap.push(-a), misum+=a;
miheap.push(-b), misum+=b;
mxheap.push(mi), mxsum+=mi;
}
else{
mxheap.push(a), mxsum+=a;
miheap.push(-b), misum+=b;
}
}
lld getsum(){
return misum-mxsum;
}
}hs;
int b, n, cnt;
lld prev_sum, heap_sum[MX], rev_sum[MX], ans;
int main(){
int i;
scanf("%d%d", &b, &n);
for(i=0; i<n; i++){
char Tsi, Tei;
lld Psi, Pei;
scanf("\n%c %lld %c %lld", &Tsi, &Psi, &Tei, &Pei);
if(Tsi == Tei)prev_sum += abs2(Psi-Pei);
else{
prev_sum++;
arr[cnt].s=Psi, arr[cnt].e=Pei;
if(arr[cnt].s > arr[cnt].e)swap(arr[cnt].s, arr[cnt].e);
cnt++;
}
}
sort(arr, arr+cnt);
for(i=0; i<cnt; i++){
hs.ins(arr[i].s, arr[i].e);
heap_sum[i]=hs.getsum();
}
hs.clear();
for(i=cnt-1; i>=0; i--){
hs.ins(arr[i].s, arr[i].e);
rev_sum[i]=hs.getsum();
}
ans=heap_sum[cnt-1];
if(b==2){
for(i=0; i<cnt-1; i++){
if(ans > heap_sum[i]+rev_sum[i+1])ans = heap_sum[i]+rev_sum[i+1];
}
}
printf("%lld\n", prev_sum+ans);
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:60:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
60 | scanf("%d%d", &b, &n);
| ~~~~~^~~~~~~~~~~~~~~~
bridge.cpp:64:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
64 | scanf("\n%c %lld %c %lld", &Tsi, &Psi, &Tei, &Pei);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 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 |
2 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 |
66 ms |
5208 KB |
Output is correct |
13 |
Correct |
98 ms |
5224 KB |
Output is correct |
14 |
Correct |
86 ms |
4608 KB |
Output is correct |
15 |
Correct |
56 ms |
3240 KB |
Output is correct |
16 |
Correct |
51 ms |
5208 KB |
Output is correct |
17 |
Correct |
83 ms |
5208 KB |
Output is correct |
18 |
Correct |
63 ms |
5208 KB |
Output is correct |
19 |
Correct |
93 ms |
5336 KB |
Output is correct |
20 |
Correct |
63 ms |
5208 KB |
Output is correct |
21 |
Correct |
86 ms |
5228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 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 |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 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 |
364 KB |
Output is correct |
15 |
Correct |
1 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 |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
2 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 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 |
364 KB |
Output is correct |
15 |
Correct |
1 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 |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
56 ms |
5976 KB |
Output is correct |
26 |
Correct |
75 ms |
6116 KB |
Output is correct |
27 |
Correct |
107 ms |
6896 KB |
Output is correct |
28 |
Correct |
100 ms |
7404 KB |
Output is correct |
29 |
Correct |
99 ms |
7464 KB |
Output is correct |
30 |
Correct |
52 ms |
4216 KB |
Output is correct |
31 |
Correct |
53 ms |
6892 KB |
Output is correct |
32 |
Correct |
84 ms |
7512 KB |
Output is correct |
33 |
Correct |
67 ms |
7128 KB |
Output is correct |
34 |
Correct |
93 ms |
7512 KB |
Output is correct |
35 |
Correct |
67 ms |
7128 KB |
Output is correct |
36 |
Correct |
88 ms |
7256 KB |
Output is correct |
37 |
Correct |
53 ms |
5976 KB |
Output is correct |