#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<pair<int,int>> v;
ll f(ll puente){
//cout << "puente: " << puente << endl;
ll res = 0;
for(auto par : v){
ll x = par.first;
ll y = par.second;
if(x <= puente && puente <= y){
res += (y-x+1);
}else{
res += (abs(puente - x) + abs(puente - y) + 1);
}
//cout << res << endl;
}
return res;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int k,n;
cin >> k >> n;
ll sol = 0;
int mayor = 0;
for(int i=0; i<n; i++){
int x,y;
char a,b;
cin >> a >> x >> b >> y;
mayor = max(mayor,x);
mayor = max(mayor,y);
if(a == b){
sol += abs(x-y);
}else{
if(x > y){
swap(x,y);
}
v.push_back({x,y});
}
}
//Ternary search
int lb = 0, ub = mayor;
while(lb+2 < ub){
//cout << lb << " " << ub << endl;
int m1 = lb + (lb+ub)/3;
int m2 = ub - (lb+ub)/3;
//cout << m1 << " " << f(m1) << " " << m2 << " " << f(m2) << endl;
if(f(m1) <= f(m2)){
ub = max(m1,m2)-1;
}else{
lb = min(m1,m2)+1;
}
}
ll res = 1e18;
for(int i=lb; i<=ub; i++){
res = min(res,f(i));
}
cout << res+sol << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Execution timed out |
2087 ms |
340 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Execution timed out |
2068 ms |
340 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |