OpenJudge/Poj 1723 SOLDIERS

1.链接地址:

http://bailian.openjudge.cn/practice/1723/

http://poj.org/problem?id=1723

2.题目:

总时间限制:
1000ms
内存限制:
65536kB
描述
N soldiers of the land Gridland are randomly scattered around the country.
A position in Gridland is given by a pair (x,y) of integer coordinates. Soldiers can move - in one move, one soldier can go one unit up, down, left or right (hence, he can change either his x or his y coordinate by 1 or -1).

The soldiers want to get into a horizontal line next to each other (so that their final positions are (x,y), (x+1,y), ..., (x+N-1,y), for some x and y). Integers x and y, as well as the final order of soldiers along the horizontal line is arbitrary.

The goal is to minimise the total number of moves of all the soldiers that takes them into such configuration.

Two or more soldiers must never occupy the same position at the same time.
输入
The first line of the input contains the integer N, 1 <= N <= 10000, the number of soldiers.
The following N lines of the input contain initial positions of the soldiers : for each i, 1 <= i <= N, the (i+1)st line of the input file contains a pair of integers x[i] and y[i] separated by a single blank character, representing the coordinates of the ith soldier, -10000 <= x[i],y[i] <= 10000.
输出
The first and the only line of the output should contain the minimum total number of moves that takes the soldiers into a horizontal line next to each other.
样例输入
5
1 2
2 2
1 3
3 -2
3 3
样例输出
8
来源
CEOI 1998

3.思路:

4.代码:

 1 //1723_01.cpp
 2 //2010-04-09 by wuzhihui
 3 
 4 
 5 #include<iostream>
 6 #include<cstdio>
 7 #include <cmath>
 8 
 9 using namespace std;
10 int x[10002],y[10002],c[10002];
11 
12 //快速排序 
13 int Partition(int a[],int low,int high)
14 {
15     int temp=a[low];
16     while(low<high)
17     {
18         while(low<high&&a[high]>=temp) high--;
19         a[low]=a[high];
20         while(low<high&&a[low]<=temp) low++;
21         a[high]=a[low];
22     }
23     a[low]=temp;
24     return low;
25 }
26 void QSort(int a[],int low ,int high)
27 {
28      int pivotloc;
29      if(low<high)
30      {
31          pivotloc=Partition(a,low,high);
32          QSort(a,low,pivotloc-1);
33          QSort(a,pivotloc+1,high);
34      }
35 }
36 void QuickSort(int a[],int size)
37 {
38      QSort(a,0,size-1);
39 }
40 
41 long long f(int x[],int y[],int size)
42 {
43     int i;
44     QuickSort(x,size);
45     for(i=0;i<size;i++)
46     {
47         c[i]=x[i]-i;
48     }
49     QuickSort(y,size);
50     QuickSort(c,size);
51     long long count=0;
52     for(i=0;i<size;i++)
53     {
54           count+=abs(y[i]-y[size/2])+abs(x[i]-(c[size/2]+i));
55     }
56     return count;
57 }
58 int main()
59 {
60     //测试快排的正确性 
61     /*int test[10]={9,8,7,6,5,4,3,2,1,0};
62     int testSize=10;
63     QuickSort(test,testSize);
64     for(int i=0;i<10;i++) {cout<<test[i]<<" ";}
65     cout<<endl; */
66     
67     
68     
69     int size;
70     int i,j;
71     long long count;
72     while((cin>>size)&&size!=0)
73     {
74         for(i=0;i<size;i++)
75         {
76             cin>>x[i]>>y[i];
77         }
78         
79         //测试输入是否正确
80         /*for(i=0;i<size;i++)
81         {
82           cout<<x[i]<<" "<<y[i];
83           cout<<endl;
84         } */
85         
86         count=f(x,y,size);
87         printf("%lld
",count);
88     }
89     
90     //system("pause");
91     return 1;
92 }
原文地址:https://www.cnblogs.com/mobileliker/p/3570515.html