이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll k,n;
vector<pair<int,pair<ll,ll>>> v,ordx,ordy;
/*
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;
}
*/
ll f2(ll puente,ll sol){
ll lo = 0, hi = ordx.size()-1;
ll res1 = ordx.size()-1,res2 = 0;
while(lo <= hi){
int mid = (lo+hi)/2;
if(ordx[mid].second.first > puente){
res1 = mid;
hi = mid-1;
}else{
lo = mid+1;
}
}
lo = 0; hi = ordy.size()-1;
while(lo <= hi){
int mid = (lo+hi)/2;
if(ordy[mid].second.second < puente){
res2 = mid;
lo = mid+1;
}else{
hi = mid-1;
}
}
vector<bool> usado(n,false);
for(int i=0; i<res1+1; i++){
ll x = ordx[i].second.first;
ll y = ordx[i].second.second;
ll ind = ordx[i].first;
sol -= (y-x+1);
sol += (abs(puente - x) + abs(puente - y) + 1);
usado[ind] = true;
}
for(int i=res2; i<(int)ordy.size(); i++){
ll ind = ordy[i].first;
if(usado[ind])continue;
ll x = ordy[i].second.first;
ll y = ordy[i].second.second;
sol -= (y-x+1);
sol += (abs(puente - x) + abs(puente - y) + 1);
}
//cout << puente << " " << sol << endl;
return sol;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
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({i,{x,y}});
sol += (y-x+1);
}
}
ordx = ordy = v;
sort(begin(ordx),end(ordx),[] (const pair<int,pair<ll,ll>> & a, const pair<int,pair<ll,ll>>& b){
return a.second.first <= b.second.first;
});
sort(begin(ordy),end(ordy),[] (const pair<int,pair<ll,ll>> & a, const pair<int,pair<ll,ll>>& b){
return a.second.second <= b.second.second;
});
vector<bool> usado(n,false);
//Ternary search
ll lb = 0, ub = 1e9;
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(f2(m1,sol) <= f2(m2,sol)){
ub = m2-1;
}else{
lb = m1+1;
}
}
ll res = 1e18;
for(ll i=lb; i<=ub; i++){
res = min(res,f2(i,sol));
}
cout << res << 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... |