标签:
2
3
1 1
2 1
1 2
5
2 9
5 20
11 9
1 1
1 20
2
44
#include<iostream> #include<algorithm> using namespace std; int x[30],y[30],n,m,i;; int main() { cin>>n; while(n--) { cin>>m; for(i=0;i<m;i++) cin>>x[i]>>y[i]; sort(x,x+m); sort(y,y+m); int sum=0; for(i=0;i<m/2;i++) sum+=x[m-1-i]-x[i]+y[m-1-i]-y[i]; cout<<sum<<endl; } return 0; }
//v0.01 C
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,m;
scanf("%d", &n);
getchar();
while (n--) {
scanf("%d", &m);
getchar();
int arr[m][2];
int sum = 0;
// input m
for (int i = 0; i < m; ++i) {
for (int j = 0; j < 2; ++j) {
scanf("%d",&arr[i][j]);
//printf("\n");
}
}
//output tests
/*
*
* for (int i = 0; i < m; ++i) {
for (int j = 0; j < 2; ++j) {
printf("%d ",arr[i][j]);
}
printf("\n");
}*/
int x=0;
//the bound of X
for (int i = 0; i < m; ++i) {
if(arr[i][0] > x){
x = arr[i][0];
}
}
printf("bound x:%d\n", x);
//the bound of Y
int y=0;
for (int i = 0; i < m; ++i) {
if(arr[i][1] > y){
y = arr[i][1];
}
}
printf("bound y:%d\n", y);
//min Xsum & Ysum
int tmp_Xmin=2000;
int tmp_Ymin=2000;
int p = 0;
int q =0;
while(p<=x){
int sumx = 0;
for (int i = 0; i < m; ++i) {
sumx += abs(arr[i][0]-p);
}
if(tmp_Xmin > sumx){
tmp_Xmin = sumx;
}
p++;
}
while(q<=y){
int sumy = 0;
for (int i = 0; i < m; ++i) {
sumy += abs(arr[i][0]-q);
}
//Y
for (int i = 0; i < m; ++i) {
sumy += abs(arr[i][1]-p);
}
if(tmp_Ymin > sumy){
tmp_Ymin = sumy;
}
q++;
}
printf("tmp_Xmin:%d\n",tmp_Xmin);
printf("tmp_Ymin:%d\n",tmp_Ymin);
sum = tmp_Xmin + tmp_Ymin;
printf("sumMIN:%d\n", sum);
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/guxuanqing/p/5585635.html