#include<bits/stdc++.h>
using namespace std;
void solve(){
int k, n, a, b;cin >> k >> n;
char c,h;
long long sl = 0, ans = LONG_LONG_MAX, cur = 0, tans = 0;
vector<pair<int, int>> e;
if(k == 1){
while(n--){
cin >> c >> a >> h >> b;
if(a>b)swap(a,b);
tans += b-a;
if(c==h) continue;
tans++;
e.emplace_back(a, false);
e.emplace_back(b, true);
}
if(e.empty()){
cout << tans << '\n';
return;
}
sort(e.begin(), e.end());
for(int i = 0;i < e.size();i++){
if(!e[i].second) {
sl -= 2;
cur += 2*(e[i].first-e[0].first);
}
}
int pr = e[0].first;
for(auto p : e){
cur += sl*(p.first-pr);
pr = p.first;
ans = min(ans, cur);
sl += 2;
}
cout << ans+tans << '\n';
return;
}
for(int i = 0;i < n;i++){
cin >> c >> a >> h >> b;
if(a>b)swap(a,b);
tans += b-a;
if(c==h) continue;
tans++;
e.emplace_back(a, -i-1);
e.emplace_back(b, i+1);
}
if(e.empty()){
cout << tans << '\n';
return;
}
sort(e.begin(), e.end());
int pr = e[0].first;
for(auto p : e){
cur += sl*(p.first-pr);
pr = p.first;
if(p.second > 0) sl += 2;
vector<pair<int, int>> g;
int v[n+1];fill(v, v+n+1, -1);
long long slope = 0, CUR = 0;
for(auto pp : e){
if(pp.first <= p.first) continue;
if(pp.second < 0){
CUR += 2*(pp.first-p.first);
slope -= 2;
g.emplace_back(pp.first, 2);
v[-pp.second] = pp.first-p.first;
} else if(v[pp.second] != -1){
g.emplace_back(pp.first, 2);
g.emplace_back(pp.first+v[pp.second], -2);
}
}
if(g.empty()){
ans = min(ans, cur);
continue;
}
sort(g.begin(), g.end());
int pre = p.first;
for(auto pp : g){
CUR += slope*(pp.first-pre);
pre = pp.first;
ans = min(ans, cur+CUR);
slope += pp.second;
}
}
cout << ans+tans << '\n';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
}
Compilation message
bridge.cpp: In function 'void solve()':
bridge.cpp:23:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(int i = 0;i < e.size();i++){
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
17 ms |
2520 KB |
Output is correct |
13 |
Correct |
35 ms |
2516 KB |
Output is correct |
14 |
Correct |
28 ms |
2520 KB |
Output is correct |
15 |
Correct |
20 ms |
1500 KB |
Output is correct |
16 |
Correct |
19 ms |
2548 KB |
Output is correct |
17 |
Correct |
23 ms |
2520 KB |
Output is correct |
18 |
Correct |
26 ms |
2520 KB |
Output is correct |
19 |
Correct |
33 ms |
2520 KB |
Output is correct |
20 |
Correct |
27 ms |
2520 KB |
Output is correct |
21 |
Correct |
31 ms |
2516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
12 ms |
348 KB |
Output is correct |
14 |
Correct |
65 ms |
344 KB |
Output is correct |
15 |
Correct |
76 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
344 KB |
Output is correct |
17 |
Correct |
17 ms |
500 KB |
Output is correct |
18 |
Correct |
10 ms |
348 KB |
Output is correct |
19 |
Correct |
4 ms |
348 KB |
Output is correct |
20 |
Correct |
92 ms |
344 KB |
Output is correct |
21 |
Correct |
36 ms |
348 KB |
Output is correct |
22 |
Correct |
90 ms |
508 KB |
Output is correct |
23 |
Correct |
29 ms |
348 KB |
Output is correct |
24 |
Correct |
90 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
12 ms |
348 KB |
Output is correct |
14 |
Correct |
65 ms |
508 KB |
Output is correct |
15 |
Correct |
82 ms |
508 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
17 ms |
348 KB |
Output is correct |
18 |
Correct |
10 ms |
348 KB |
Output is correct |
19 |
Correct |
4 ms |
344 KB |
Output is correct |
20 |
Correct |
95 ms |
508 KB |
Output is correct |
21 |
Correct |
34 ms |
348 KB |
Output is correct |
22 |
Correct |
90 ms |
344 KB |
Output is correct |
23 |
Correct |
29 ms |
344 KB |
Output is correct |
24 |
Correct |
89 ms |
508 KB |
Output is correct |
25 |
Execution timed out |
2011 ms |
4436 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |