标签:des style blog http io color ar os sp
Description
Input
Output
Sample Input
6 1 1 1 3 3 3 2 1 3 2 2 2 0
Sample Output
The length of the fence will be 8 units.
题目大意:就是数字是柱子的坐标,没两根柱子之间有栅栏 求栅栏的长度。。。
解题思路:两次排序 第一次按X排,第二次按y排,,,,然后分别把差值加起来
1 #include<stdio.h> 2 #include<algorithm> 3 #include<math.h> 4 using namespace std; 5 struct weizhi{ 6 int x; 7 int y; 8 }a[200000]; 9 bool cmp1(weizhi a,weizhi b) 10 { 11 if(a.x==b.x) 12 return a.y<b.y; 13 return a.x<b.x; 14 } 15 bool cmp2(weizhi a,weizhi b) 16 { 17 if(a.y==b.y) 18 return a.x<b.x; 19 return a.y<b.y; 20 } 21 int main() 22 { 23 long int n; 24 while(scanf("%d",&n)!=EOF) 25 { 26 if(n==0)break; 27 int i; 28 for(i=0;i<n;i++) 29 { 30 scanf("%d %d",&a[i].x,&a[i].y); 31 } 32 int sum=0; 33 sort(a,a+n,cmp1); 34 for(i=0;i<n;i+=2) 35 { 36 sum+=a[i+1].y-a[i].y; 37 } 38 sort(a,a+n,cmp2); 39 for(i=0;i<n;i+=2) 40 { 41 sum+=a[i+1].x-a[i].x; 42 } 43 printf("The length of the fence will be %d units.\n",sum); 44 } 45 return 0; 46 }
POJ 1788 Building a New Depot 解题报告
标签:des style blog http io color ar os sp
原文地址:http://www.cnblogs.com/wangrunwen/p/4083160.html