이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
#define int long long
map<int,int>bit[2];
void add(int t,int k,int x){
k++;
while(k<=1e9+1){
bit[t][k]+=x;
k+=k&-k;
}
}
int query(int t,int k){
k++;
int res=0;
while(k){
res+=bit[t][k];
k-=k&-k;
}
return res;
}
char p[200000][2],q[200000][2];
int s[200000],t[200000];
signed main(){
int K,n;cin>>K>>n;
if(K==1){
vector<int>v;
ll sum=0;
rep(i,n){
scanf("%s%lld%s%lld",p[i],&s[i],q[i],&t[i]);
if(p[i][0]==q[i][0]){
sum+=abs(s[i]-t[i]);
continue;
}
sum++;
v.push_back(s[i]);
v.push_back(t[i]);
}
sort(v.begin(),v.end());
if(!v.empty()){
int md=v[v.size()/2];
for(int i:v)sum+=abs(i-md);
}
cout<<sum<<endl;
return 0;
}
ll sum=0;
vector<P>v;
vector<int>xs;
rep(i,n){
scanf("%s%lld%s%lld",p[i],&s[i],q[i],&t[i]);
if(p[i][0]==q[i][0]){
sum+=abs(s[i]-t[i]);
continue;
}
sum++;
v.push_back(P(min(s[i],t[i]),max(s[i],t[i])));
xs.push_back(s[i]);
xs.push_back(t[i]);
}
if(v.empty()){
cout<<sum<<endl;
return 0;
}
sort(xs.begin(),xs.end());
xs.erase(unique(xs.begin(),xs.end()),xs.end());
ll Min=LLONG_MAX;
for(int i:xs){
bit[0].clear();
bit[1].clear();
ll cnt=0;
for(auto p:v){
if(p.first<=i){
cnt+=abs(p.first-i)+abs(p.second-i);
continue;
}
add(1,0,abs(p.first-i)+abs(p.second-i));// a
add(1,i,-abs(p.first-i)-abs(p.second-i));// a'
add(0,i,-2);// b
add(1,i,abs(p.first-i)+abs(p.second-i)-(-2*i));// c
add(0,p.first,2);// b'
add(1,p.first,-abs(p.first-i)-abs(p.second-i)+(-2*i));// c'
add(1,p.first,p.second-p.first);// d
add(1,p.second,p.first-p.second);// d'
add(0,p.second,2);//e
add(1,p.second,(p.second-p.first)-2*p.second);//f
}
ll d=LLONG_MAX;
for(int j:xs){
d=min(d,query(0,j)*(ll)j+query(1,j));
}
Min=min(Min,cnt+d);
}
/*
ll Min=LLONG_MAX;
rep(i,xs.size())rep(j,xs.size()){
ll cnt=0;
rep(k,v.size()){
cnt+=min(abs(xs[i]-v[k].first)+abs(xs[i]-v[k].second),
abs(xs[j]-v[k].first)+abs(xs[j]-v[k].second));
}
Min=min(Min,cnt);
}*/
cout<<sum+Min<<endl;
}
컴파일 시 표준 에러 (stderr) 메시지
bridge.cpp: In function 'int main()':
bridge.cpp:34:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s%lld%s%lld",p[i],&s[i],q[i],&t[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:55:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s%lld%s%lld",p[i],&s[i],q[i],&t[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |