#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long ll;
const ll inf = 1e18+10;
typedef pair<ll,ll> pii;
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int k, n;
cin >> k >> n;
ll ans1 = 0, ans2 = 0;
vector<ll> a, b, c;
for(int i=1; i<=n; i++){
int s, t;
char p, q;
cin >> p >> s >> q >> t;
if(p == q) ans1 += abs(s - t);
else{
if(p == 'B') swap(s, t);
if(s > t) swap(s, t);
a.push_back(s);
b.push_back(t);
}
}
for(auto x : a) c.push_back(x);
for(auto x : b) c.push_back(x);
if(k == 1){
for(auto x : a) c.push_back(x);
for(auto x : b) c.push_back(x);
sort(c.begin(), c.end());
if(!c.empty()){
ll opt = c[(int)c.size()/2];
for(auto x : a) ans2 += abs(opt - x);
for(auto x : b) ans2 += abs(opt - x);
}
}
else if(!c.empty()){
ans2 = inf;
for(int i=0; i<c.size(); i++){
priority_queue<pii, vector<pii>, greater<pii>> fila;
ll g = 0, delta = 0;
for(int j=0; j<a.size(); j++){
if(a[j] <= c[i] and c[i] <= b[j]) g += b[j] - a[j];
else{
ll gap = min(abs(a[j] - c[i]), abs(b[j] - c[i]));
g += 2*gap+b[j]-a[j];
fila.push({a[j], +2});
fila.push({b[j], +2});
fila.push({a[j] - gap, -2});
fila.push({b[j] + gap, -2});
}
}
ll x = -inf;
ans2 = min(ans2, g);
while(!fila.empty()){
pii event = fila.top();
fila.pop();
g += delta*(event.f - x);
ans2 = min(ans2, g);
x = event.f;
delta += event.s;
}
}
}
cout << ans1 + ans2 + (int)a.size() << "\n";
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:40:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<c.size(); i++){
~^~~~~~~~~
bridge.cpp:43:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0; j<a.size(); j++){
~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
6 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
37 ms |
6112 KB |
Output is correct |
13 |
Correct |
78 ms |
6252 KB |
Output is correct |
14 |
Correct |
57 ms |
5984 KB |
Output is correct |
15 |
Correct |
47 ms |
3440 KB |
Output is correct |
16 |
Correct |
42 ms |
6120 KB |
Output is correct |
17 |
Correct |
70 ms |
6112 KB |
Output is correct |
18 |
Correct |
86 ms |
6108 KB |
Output is correct |
19 |
Correct |
79 ms |
6112 KB |
Output is correct |
20 |
Correct |
49 ms |
6112 KB |
Output is correct |
21 |
Correct |
74 ms |
6112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
8 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
11 ms |
384 KB |
Output is correct |
9 |
Correct |
7 ms |
384 KB |
Output is correct |
10 |
Correct |
12 ms |
384 KB |
Output is correct |
11 |
Correct |
8 ms |
384 KB |
Output is correct |
12 |
Correct |
11 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
8 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
11 ms |
384 KB |
Output is correct |
9 |
Correct |
7 ms |
384 KB |
Output is correct |
10 |
Correct |
11 ms |
384 KB |
Output is correct |
11 |
Correct |
8 ms |
384 KB |
Output is correct |
12 |
Correct |
11 ms |
384 KB |
Output is correct |
13 |
Correct |
122 ms |
384 KB |
Output is correct |
14 |
Correct |
599 ms |
512 KB |
Output is correct |
15 |
Correct |
552 ms |
512 KB |
Output is correct |
16 |
Correct |
15 ms |
384 KB |
Output is correct |
17 |
Correct |
173 ms |
760 KB |
Output is correct |
18 |
Correct |
77 ms |
384 KB |
Output is correct |
19 |
Correct |
8 ms |
384 KB |
Output is correct |
20 |
Correct |
753 ms |
568 KB |
Output is correct |
21 |
Correct |
313 ms |
512 KB |
Output is correct |
22 |
Correct |
878 ms |
632 KB |
Output is correct |
23 |
Correct |
456 ms |
608 KB |
Output is correct |
24 |
Correct |
835 ms |
560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
8 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
10 ms |
384 KB |
Output is correct |
9 |
Correct |
7 ms |
384 KB |
Output is correct |
10 |
Correct |
11 ms |
384 KB |
Output is correct |
11 |
Correct |
8 ms |
384 KB |
Output is correct |
12 |
Correct |
11 ms |
384 KB |
Output is correct |
13 |
Correct |
119 ms |
384 KB |
Output is correct |
14 |
Correct |
595 ms |
632 KB |
Output is correct |
15 |
Correct |
550 ms |
632 KB |
Output is correct |
16 |
Correct |
14 ms |
384 KB |
Output is correct |
17 |
Correct |
171 ms |
512 KB |
Output is correct |
18 |
Correct |
76 ms |
384 KB |
Output is correct |
19 |
Correct |
8 ms |
384 KB |
Output is correct |
20 |
Correct |
751 ms |
568 KB |
Output is correct |
21 |
Correct |
312 ms |
512 KB |
Output is correct |
22 |
Correct |
865 ms |
568 KB |
Output is correct |
23 |
Correct |
451 ms |
512 KB |
Output is correct |
24 |
Correct |
839 ms |
528 KB |
Output is correct |
25 |
Execution timed out |
2092 ms |
8148 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |