이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
int gcd(int a, int b) {return b == 0 ? a : gcd(b, a % b);}
#define ll long long
#define pb push_back
#define ld long double
#define mp make_pair
#define F first
#define S second
#define pii pair<ll,ll>
using namespace :: std;
const ll maxn=1e5+500;
const ll inf=1e9+800;
ll l[maxn];
ll r[maxn];
ll n,k;
ll mn=inf,mx=0;
ll findSumD(ll L,ll R){
ll ans=0;
for(ll i=0;i<n;i++){
if((l[i]<=L && L<=r[i])|| (l[i]<=R && R<=r[i])){
continue;
}
if(l[i]<L){
ans+=2*(L-r[i]);
}
else if(R<l[i]){
ans+=2*(l[i]-R);
}else{
ans+=2*min(l[i]-L,R-r[i]);
}
}
return ans;
}
ll findAns(ll mid){
if(k==1){
return findSumD(mid,2*inf);
}
ll ans=inf*maxn;
ll b=mid+1;
ll e=mx;
while(e-b>2){
ll mid1=b+(e-b)/3;
ll mid2=b+(e-b)*2/3;
ll g1=findSumD(mid,mid1);
ll g2=findSumD(mid,mid2);
ans=min(ans,g1);
ans=min(ans,g2);
if(g1<g2){
e=mid2;
}else{
b=mid1;
}
}
for(ll i=b;i<=e;i++){
ans=min(ans,findSumD(mid,i));
}
b=mn;
e=mid;
while(e-b>2){
ll mid1=b+(e-b)/3;
ll mid2=b+(e-b)*2/3;
ll g1=findSumD(mid1,mid);
ll g2=findSumD(mid2,mid);
ans=min(ans,g1);
ans=min(ans,g2);
if(g1<g2){
e=mid2;
}else{
b=mid1;
}
}
for(ll i=b;i<=e;i++){
ans=min(ans,findSumD(i,mid));
}
return ans;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>k>>n;
ll sum=0;
for(ll i=0;i<n;i++){
char C,T;
ll L,R;
cin>>C>>L>>T>>R;
sum+=(abs(R-L));
if(C==T){
n--;
i--;
}else{
sum++;
l[i]=min(L,R);
r[i]=max(L,R);
mn=min(mn,l[i]);
mx=max(mx,r[i]);
}
}
ll b=mn;
ll e=mx;
ll ans=inf*maxn;
while(e-b>2){
ll mid1=b+(e-b)/3;
ll mid2=b+(e-b)*2/3;
ll g1=findAns(mid1);
ll g2=findAns(mid2);
ans=min(ans,g1);
ans=min(ans,g2);
if(g1<g2){
e=mid2;
}else{
b=mid1;
}
}
for(ll i=b;i<=e;i++){
ans=min(ans,findAns(i));
}
cout<<ans+sum;
}
# | 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... |