이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
int const inf=1e16;
int k,n;
void solve(){
vector<int> pos;
int ans=0;
vector<pair<int,int>> pr;
for (int i = 0; i < n; ++i)
{
char a,b;
int pa,pb;
cin>>a>>pa>>b>>pb;
if(a==b)
ans+=abs(pa-pb);
else{
if(pa>pb)
swap(pa,pb);
pr.push_back({pa,pb});
pos.push_back(pa);
pos.push_back(pb);
}
}
ans+=pr.size();
int ans1=inf;
if(pr.size()==0)
ans1=0;
for (auto b1:pos)
for(auto b2:pos){
if(b1>=b2)
continue;
int temp=0;
for(auto p:pr)
temp+=min( abs(b1-p.first)+abs(b1-p.second) , abs(b2-p.first)+abs(b2-p.second));
ans1=min(ans1,temp);
}
cout<<ans+ans1<<endl;
}
signed main(){
cin>>k>>n;
if(k==2){
solve();
return 0;
}
vector<int> pos;
int ans=0;
int sm=0;
for (int i = 0; i < n; ++i)
{
char a,b;
int pa,pb;
cin>>a>>pa>>b>>pb;
if(a==b)
ans+=abs(pa-pb);
else{
sm+=pa;
sm+=pb;
pos.push_back(pa);
pos.push_back(pb);
}
}
sort(pos.begin(), pos.end());
int ex=0;
int cnta=0;
ans+=(pos.size())/2;
int cntb=pos.size();
int ans1=sm;
for (auto i:pos)
{
// cout<<i<<' ';
sm-=i;
ex+=i;
cnta++;
cntb--;
int temp=(i*cnta)-ex;
temp+=sm-(i*cntb);
// cout<<temp<<endl;
ans1=min(ans1,temp);
}
cout<<ans+ans1<<endl;
return 0;
}
# | 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... |