이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<pair<ll,ll>> 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);
ll k,n;
cin >> k >> n;
ll sol = 0;
ll mayor = 0;
for(int i=0; i<n; i++){
ll 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
ll lb = 0, ub = mayor;
while(lb+2 < ub){
//cout << lb << " " << ub << endl;
ll m1 = lb + (lb+ub)/3;
ll m2 = ub - (lb+ub)/3;
//cout << m1 << " " << f(m1) << " " << m2 << " " << f(m2) << endl;
if(m1 > m2)break;
if(f(m1) <= f(m2)){
ub = m2-1;
}else{
lb = m1+1;
}
}
ll res = 1e18;
for(ll i=lb; i<=ub; i++){
res = min(res,f(i));
}
cout << res+sol << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |