#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
using namespace std;
typedef long long ll;
const int mx = 100005;
int k, n, m;
ll D[mx], E[mx];
struct People{
int a, b;
bool operator < (People z){
return a + b < z.a + z.b;
}
} P[mx];
void subpr(ll *V){
if(!n) return;
ll dval = abs(P[0].a - P[0].b);
priority_queue<int> lft;
priority_queue<int,vector<int>,greater<int>> rgt;
int brp = P[0].a;
(P[0].b < brp ? lft.push(P[0].b) : rgt.push(P[0].b));
V[0] = dval;
//printf("%lld\n",V[0]);
for(int i = 1; i < n; i++){
//cout << "i = " << i << endl;
dval += abs(brp - P[i].a) + abs(brp - P[i].b);
P[i].a < brp ? lft.push(P[i].a) : rgt.push(P[i].a);
P[i].b < brp ? lft.push(P[i].b) : rgt.push(P[i].b);
while(sz(lft) < sz(rgt)){
int nbr = rgt.top(); rgt.pop();
//cout << brp << " -> " << nbr << endl;
lft.push(brp);
dval += (ll)(nbr - brp) * (sz(lft) - sz(rgt) - 1);
brp = nbr;
}
while(sz(lft) > sz(rgt) + 1){
int nbr = lft.top(); lft.pop();
//cout << brp << " -> " << nbr << endl;
rgt.push(brp);
dval += (ll)(brp - nbr) * (sz(rgt) - sz(lft) - 1);
brp = nbr;
}
V[i] = dval;
//printf("%d %lld\n",brp,V[i]);
}
}
ll off;
void input(){
scanf("%d %d",&k,&m);
while(m--){
char p,q;
int a,b;
scanf("%*c%c %d %c %d",&p,&a,&q,&b);
//printf("m = %d, %c %d %c %d\n",m,p,a,q,b);
if(p == q) off += abs(a - b);
else P[n++] = {a, b};
}
}
int main(){
input();
sort(P,P+n);
//for(int i = 0; i < n; i++) printf("%d %d\n",P[i].a,P[i].b);
subpr(D);
if(k == 1) return !printf("%lld\n",D[n-1] + off + n);
reverse(P,P+n);
subpr(E);
ll ans = min(D[n-1], E[n-1]);
for(int i = 0; i < n - 1; i++){
ans = min(ans, D[i] + E[n - 2 - i]);
}
printf("%lld\n",ans + off + n);
}
Compilation message
bridge.cpp: In function 'void input()':
bridge.cpp:54:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&k,&m);
^
bridge.cpp:58:44: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%*c%c %d %c %d",&p,&a,&q,&b);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
1 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
3 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
704 KB |
Output is correct |
6 |
Correct |
2 ms |
720 KB |
Output is correct |
7 |
Correct |
2 ms |
776 KB |
Output is correct |
8 |
Correct |
2 ms |
848 KB |
Output is correct |
9 |
Correct |
2 ms |
884 KB |
Output is correct |
10 |
Correct |
2 ms |
908 KB |
Output is correct |
11 |
Correct |
2 ms |
908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
908 KB |
Output is correct |
2 |
Correct |
2 ms |
908 KB |
Output is correct |
3 |
Correct |
2 ms |
956 KB |
Output is correct |
4 |
Correct |
2 ms |
980 KB |
Output is correct |
5 |
Correct |
2 ms |
1004 KB |
Output is correct |
6 |
Correct |
2 ms |
1028 KB |
Output is correct |
7 |
Correct |
2 ms |
1048 KB |
Output is correct |
8 |
Correct |
2 ms |
1076 KB |
Output is correct |
9 |
Correct |
2 ms |
1096 KB |
Output is correct |
10 |
Correct |
2 ms |
1120 KB |
Output is correct |
11 |
Correct |
2 ms |
1140 KB |
Output is correct |
12 |
Correct |
40 ms |
4476 KB |
Output is correct |
13 |
Correct |
76 ms |
6708 KB |
Output is correct |
14 |
Correct |
62 ms |
7732 KB |
Output is correct |
15 |
Correct |
45 ms |
8324 KB |
Output is correct |
16 |
Correct |
40 ms |
11060 KB |
Output is correct |
17 |
Correct |
62 ms |
13364 KB |
Output is correct |
18 |
Correct |
46 ms |
15232 KB |
Output is correct |
19 |
Correct |
73 ms |
17648 KB |
Output is correct |
20 |
Correct |
49 ms |
19588 KB |
Output is correct |
21 |
Correct |
65 ms |
21624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
21624 KB |
Output is correct |
2 |
Correct |
1 ms |
21624 KB |
Output is correct |
3 |
Correct |
2 ms |
21624 KB |
Output is correct |
4 |
Correct |
2 ms |
21624 KB |
Output is correct |
5 |
Correct |
2 ms |
21624 KB |
Output is correct |
6 |
Correct |
2 ms |
21624 KB |
Output is correct |
7 |
Correct |
2 ms |
21624 KB |
Output is correct |
8 |
Correct |
2 ms |
21624 KB |
Output is correct |
9 |
Correct |
2 ms |
21624 KB |
Output is correct |
10 |
Correct |
2 ms |
21624 KB |
Output is correct |
11 |
Correct |
2 ms |
21624 KB |
Output is correct |
12 |
Correct |
2 ms |
21624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
21624 KB |
Output is correct |
2 |
Correct |
1 ms |
21624 KB |
Output is correct |
3 |
Correct |
2 ms |
21624 KB |
Output is correct |
4 |
Correct |
2 ms |
21624 KB |
Output is correct |
5 |
Correct |
2 ms |
21624 KB |
Output is correct |
6 |
Correct |
2 ms |
21624 KB |
Output is correct |
7 |
Correct |
2 ms |
21624 KB |
Output is correct |
8 |
Correct |
2 ms |
21624 KB |
Output is correct |
9 |
Correct |
2 ms |
21624 KB |
Output is correct |
10 |
Correct |
2 ms |
21624 KB |
Output is correct |
11 |
Correct |
2 ms |
21624 KB |
Output is correct |
12 |
Correct |
2 ms |
21624 KB |
Output is correct |
13 |
Correct |
2 ms |
21624 KB |
Output is correct |
14 |
Correct |
2 ms |
21624 KB |
Output is correct |
15 |
Correct |
2 ms |
21624 KB |
Output is correct |
16 |
Correct |
2 ms |
21624 KB |
Output is correct |
17 |
Correct |
2 ms |
21624 KB |
Output is correct |
18 |
Correct |
2 ms |
21624 KB |
Output is correct |
19 |
Correct |
3 ms |
21624 KB |
Output is correct |
20 |
Correct |
3 ms |
21624 KB |
Output is correct |
21 |
Correct |
2 ms |
21624 KB |
Output is correct |
22 |
Correct |
2 ms |
21624 KB |
Output is correct |
23 |
Correct |
2 ms |
21624 KB |
Output is correct |
24 |
Correct |
2 ms |
21624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
21624 KB |
Output is correct |
2 |
Correct |
2 ms |
21624 KB |
Output is correct |
3 |
Correct |
2 ms |
21624 KB |
Output is correct |
4 |
Correct |
2 ms |
21624 KB |
Output is correct |
5 |
Correct |
2 ms |
21624 KB |
Output is correct |
6 |
Correct |
2 ms |
21624 KB |
Output is correct |
7 |
Correct |
2 ms |
21624 KB |
Output is correct |
8 |
Correct |
2 ms |
21624 KB |
Output is correct |
9 |
Correct |
2 ms |
21624 KB |
Output is correct |
10 |
Correct |
2 ms |
21624 KB |
Output is correct |
11 |
Correct |
2 ms |
21624 KB |
Output is correct |
12 |
Correct |
2 ms |
21624 KB |
Output is correct |
13 |
Correct |
2 ms |
21624 KB |
Output is correct |
14 |
Correct |
2 ms |
21624 KB |
Output is correct |
15 |
Correct |
3 ms |
21624 KB |
Output is correct |
16 |
Correct |
2 ms |
21624 KB |
Output is correct |
17 |
Correct |
2 ms |
21624 KB |
Output is correct |
18 |
Correct |
2 ms |
21624 KB |
Output is correct |
19 |
Correct |
2 ms |
21624 KB |
Output is correct |
20 |
Correct |
2 ms |
21624 KB |
Output is correct |
21 |
Correct |
2 ms |
21624 KB |
Output is correct |
22 |
Correct |
2 ms |
21624 KB |
Output is correct |
23 |
Correct |
2 ms |
21624 KB |
Output is correct |
24 |
Correct |
2 ms |
21624 KB |
Output is correct |
25 |
Correct |
45 ms |
24016 KB |
Output is correct |
26 |
Correct |
73 ms |
24916 KB |
Output is correct |
27 |
Correct |
84 ms |
26708 KB |
Output is correct |
28 |
Correct |
91 ms |
29160 KB |
Output is correct |
29 |
Correct |
95 ms |
31296 KB |
Output is correct |
30 |
Correct |
51 ms |
31296 KB |
Output is correct |
31 |
Correct |
54 ms |
34228 KB |
Output is correct |
32 |
Correct |
77 ms |
36600 KB |
Output is correct |
33 |
Correct |
52 ms |
38536 KB |
Output is correct |
34 |
Correct |
88 ms |
40924 KB |
Output is correct |
35 |
Correct |
72 ms |
43188 KB |
Output is correct |
36 |
Correct |
82 ms |
44808 KB |
Output is correct |
37 |
Correct |
40 ms |
45632 KB |
Output is correct |