#include<bits/stdc++.h>
#define all(aa) aa.begin(), aa.end()
#define endl ("\n")
#define pb push_back
typedef long long ll;
const int maxn = 1e5 + 5;
using namespace std;
int n, k;
ll rem, cur, ans = LONG_LONG_MAX;
vector<pair<ll, ll>> v;
vector<ll> u;
ll mindist(pair<ll, ll> p, ll l, ll r){
if( (p.first <= l && l <= p.second ) || (p.first <= r && r <= p.second) )
return 0;
ll ret = min(abs(l - p.first), abs(l - p.second));
ret = min(ret, abs(r - p.first));
ret = min(ret, abs(r - p.second));
return 2*ret;
}
int main(){
cin >> k >> n;
for(int i = 0; i < n; i++){
char c1, c2;
int a, b;
cin >> c1 >> a >> c2 >> b;
if(a < b) swap(a, b);
rem+= a - b;
if(c1 != c2){
rem++;
v.pb({b, a});
u.pb(a), u.pb(b);
}
}
if(k == 2){
for(int l = 0; l < u.size(); l++){
for(int r = l; r < u.size(); r++){
cur = 0;
for(auto p : v){
cur += mindist(p, l, r);
}
ans = min(ans, cur);
}
}
cout << (ans==LONG_LONG_MAX?0:ans) + rem << endl;
} else{
int n = v.size();
cur = rem;
ll rgt = n;
ll lst = 0, lft = 0;
multiset<pair<ll, ll>> come;
multiset<ll> vs;
vector<ll> u;
for(int i = 0; i < n; i++){
come.insert(v[i]);
cur+=2*v[i].first;
u.pb(v[i].first), u.pb(v[i].second);
}
sort(all(u));
sort(all(v));
ans = cur;
for(int i = 0; i < 2*n; i++){
cur += lft*2*(u[i] - lst) - rgt*2*(u[i] - lst);
while(!come.empty() && (*come.begin()).first == u[i]){
pair<ll, ll> p = *come.begin();
vs.insert({p.second});
come.erase(come.find(p));
rgt--;
}
while(!vs.empty() && *vs.begin() == u[i]){
lft++;
vs.erase(vs.begin());
}
ans = min(ans, cur);
lst = u[i];
}
cout << ans << endl;
}
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:38:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int l = 0; l < u.size(); l++){
| ~~^~~~~~~~~~
bridge.cpp:39:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int r = l; r < u.size(); r++){
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 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 |
344 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 |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
600 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 |
67 ms |
11396 KB |
Output is correct |
13 |
Correct |
154 ms |
11308 KB |
Output is correct |
14 |
Correct |
109 ms |
10160 KB |
Output is correct |
15 |
Correct |
90 ms |
6844 KB |
Output is correct |
16 |
Correct |
84 ms |
11444 KB |
Output is correct |
17 |
Correct |
94 ms |
11184 KB |
Output is correct |
18 |
Correct |
104 ms |
11440 KB |
Output is correct |
19 |
Correct |
119 ms |
11196 KB |
Output is correct |
20 |
Correct |
94 ms |
11392 KB |
Output is correct |
21 |
Correct |
106 ms |
11180 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 |
5 ms |
348 KB |
Output is correct |
4 |
Incorrect |
9 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
5 ms |
348 KB |
Output is correct |
4 |
Incorrect |
6 ms |
436 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
5 ms |
348 KB |
Output is correct |
4 |
Incorrect |
6 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |