이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr);
using namespace std;
struct line{
char x,z;
long long y,t;
}a[100009];
struct lena{
long long y,t;
}b[100009];
long long pf[100009];
vector<long long>c;
bool cmp(const lena &p, const lena &q){
return p.y+p.t<q.y+q.t;
}
int main()
{
fastio
int k,n;
cin>>k>>n;
int cnt=0;
long long ans=0;
for (int i=1;i<=n;i++){
cin>>a[i].x>>a[i].y>>a[i].z>>a[i].t;
if (a[i].x!=a[i].z){
b[++cnt]={a[i].y,a[i].t};
c.push_back(a[i].y);
c.push_back(a[i].t);
ans++;
}
else {
ans+=abs(a[i].y-a[i].t);
}
}
if (k==1){
sort(c.begin(),c.end());
int d=(int)c.size();
d/=2;
for (int i=0;i<(int)c.size();i++){
if (c[i]<=c[d]){
ans=ans+c[d]-c[i];
}
else ans=ans+c[i]-c[d];
}
cout<<ans;
return 0;
}
else {
sort(b+1,b+1+cnt,cmp);
for (int i=1;i<cnt;i++){
vector<long long>d;
for (int j=1;j<=i;j++){
d.push_back(b[j].y);
d.push_back(b[j].t);
}
sort(d.begin(),d.end());
int r=(int)d.size();
r/=2;
for (auto v:d){
if (v<=d[r])pf[i]+=d[r]-v;
else pf[i]+=v-d[r];
}
}
long long cal=1e16;
for (int i=cnt;i>1;i--){
vector<long long>d;
for (int j=cnt;j>=i;j--){
d.push_back(b[j].y);
d.push_back(b[j].t);
}
sort(d.begin(),d.end());
int r=(int)d.size();
long long gm=0;
r/=2;
for (auto v:d){
if (d[r]>=v)gm+=d[r]-v;
else gm+=v-d[r];
}
cal=min(cal,gm+pf[i-1]+ans);
}
cout<<cal;
}
/*
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
}
# | 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... |