题目链接:Easy Problem
确实是Easy Problem啊,T组数据,每组一个n,代表n个点,分别给出x坐标和y坐标,然后现在需要做的是把所有点的y轴的值放一样,然后x轴上的值连续,直接排一下序,y轴上的值全部变成和中点一样(不需要考虑奇偶,结果都一样),x轴的值从中点往前扫然后递减,从中点往后扫递增,这样结果就是最优的啦,然后这题一开始的思路确实是正确的,然后代码写的特别挫,出了写bug,后来重写才对了,刚开始还把x轴y轴的平行方式看错了
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct POINT{
int x,y;
}point[100005];
bool cmp(POINT a,POINT b){
return a.y < b.y;
}
bool cmp2(POINT a,POINT b){
return a.x < b.x;
}
int main(){
ios::sync_with_stdio(false);
int t,n;
cin>>t;
while(t--){
cin>>n;
for(int i = 0;i < n;i++)
cin>>point[i].x>>point[i].y;
sort(point,point+n,cmp);
ll sum1 = 0;
int mid = n/2;
for(int i = 0;i < n;i++)
sum1 += abs(point[i].y-point[mid].y);
sort(point,point+n,cmp2);
ll sum2 = 0;
for(int i = mid-1;i >= 0;i--){
sum2+=abs(point[i].x-(point[mid].x-(mid-i)));
}
for(int i = mid+1;i < n;i++){
sum2+=abs(point[i].x-(point[mid].x+(i-mid)));
}
cout<<sum2+sum1<<endl;
}
return 0;
}