#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
const int inf = 1e9 * 2;
const int mod = 1e9 + 7;
const int N = 3e4 + 5;
ll dis(pair<ll, ll> i, ll p){
if(i.first <= p && i.second >= p) return 0;
else if(i.first > p) return i.first - p;
else return p - i.second;
}
vector<pair<int, int> > a;
ll check(int m){
ll val = 0;
for(auto itr: a){
val += dis(itr, m) * 2;
}
return val;
}
ll check2(int m, int p){
ll val = 0;
for(auto itr: a){
val += min(dis(itr, m), dis(itr, p)) * 2;
}
return val;
}
int32_t main(){
ios_base::sync_with_stdio(false), cin.tie();
int k, n;
cin>>k>>n;
ll ans = 0;
char h, o;
int hx, ox;
for(int i = 0; i < n; i++){
cin>>h>>hx>>o>>ox;
if(h != o){
if(hx > ox) swap(hx, ox);
a.pb(mp(hx, ox));
}
ans += abs(hx - ox);
}
ans += a.size();
if(!a.size()){
cout<<ans<<endl;
}
else if(k == 1){
int l = 0, r = mod;
while(l < r){
int m = (l + r)/2;
ll x = check(m), y = check(m+1);
if(x <= y) r = m;
else l = m + 1;
}
ans += check(l);
cout<<ans<<endl;
}
else{
vector<int> points;
for(auto itr: a){
points.pb(itr.first);
points.pb(itr.second);
}
sort(points.begin(), points.end());
int add = inf;
for(int i = 0; i < points.size(); i++){
int p = points[i];
int l = 0, r = p;
while(l < r){
int m = (l + r)/2;
int x = check2(m, p), y = check2(m+1, p);
if(x <= y) r = m;
else l = m+1;
}
add = min(add, check2(l, p));
l = p, r = mod-7;
while(l < r){
int m = (l + r)/2;
int x = check2(m, p), y = check2(m+1, p);
if(x < y) r = m;
else l = m+1;
}
add = min(add, check2(l, p));
}
ans += add;
cout<<ans<<endl;
}
return 0;
}
Compilation message
bridge.cpp: In function 'int32_t main()':
bridge.cpp:84:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int i = 0; i < points.size(); i++){
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
364 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
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 |
18 ms |
2520 KB |
Output is correct |
13 |
Correct |
47 ms |
2520 KB |
Output is correct |
14 |
Correct |
27 ms |
2520 KB |
Output is correct |
15 |
Correct |
27 ms |
1500 KB |
Output is correct |
16 |
Correct |
20 ms |
2520 KB |
Output is correct |
17 |
Correct |
23 ms |
2568 KB |
Output is correct |
18 |
Correct |
22 ms |
2564 KB |
Output is correct |
19 |
Correct |
47 ms |
2464 KB |
Output is correct |
20 |
Correct |
41 ms |
2816 KB |
Output is correct |
21 |
Correct |
41 ms |
2520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Incorrect |
4 ms |
464 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
344 KB |
Output is correct |
4 |
Incorrect |
4 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Incorrect |
4 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |