This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<limits.h>
#include<math.h>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<iomanip>
#include<cstring>
#include<random>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
vector<pair<int,int>>arr;
ll eval(int br){
ll res=0;
for(auto[i,j]:arr)
res+=2*max(0,max(i-br,br-j));
return res;
}
ll eval2(int br1,int br2){
ll res=0;
for(auto[i,j]:arr)
res+=2*max(0,min(max(i-br1,br1-j),max(i-br2,br2-j)));
return res;
}
ll eval3(int br){
int l=br,r=1e9+1;
while(l<r-1){
int m=(l+r)/2;
if(eval2(br,m)>eval2(br,m+1))
l=m;
else
r=m;
}
return eval2(br,r);
}
void solve(){
int k,n;
cin>>k>>n;
ll res1=0;
for(int i=0;i<n;i++){
char c1,c2;
int a1,a2;
cin>>c1>>a1>>c2>>a2;
res1+=abs(a1-a2);
if(c1!=c2){
res1++;
if(a1>a2)
swap(a1,a2);
arr.emplace_back(a1,a2);
}
}
vector<int>pos;
for(auto[i,j]:arr){
pos.push_back(i);
pos.push_back(j);
}
sort(pos.begin(),pos.end());
ll res2=1e18;
if(k==1){
int l=-1,r=1e9+1;
while(l<r-1){
int m=(l+r)/2;
if(eval(m)>eval(m+1))
l=m;
else
r=m;
}
res2=eval(r);
}else{ // k == 2
int l=-1,r=1e9+1;
while(l<r-1){
int m=(l+r)/2;
if(eval3(m)>eval3(m+1))
l=m;
else
r=m;
}
res2=eval3(r);
}
cout<<res1+res2<<"\n";
}
int main(){
ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
//freopen("/Users/jakob/cp_testing/test.txt","r",stdin);//freopen("output.out","w",stdout);
int t=1;//cin>>t;
while(t--)solve();
return 0;
}
/*
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
24
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
22
*/
# | 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... |