POJ 1788

求多边形周长。角都是直角。

 

View Code
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 
 7 #define N 222222
 8 
 9 using namespace std;
10 
11 struct PO
12 {
13     int x,y;
14 }p[N];
15 
16 int n;
17 
18 inline bool cmpx(const PO &a,const PO &b)
19 {
20     if(a.x==b.x) return a.y<b.y;
21     return a.x<b.x;
22 }
23 
24 inline bool cmpy(const PO &a,const PO &b)
25 {
26     if(a.y==b.y) return a.x<b.x;
27     return a.y<b.y;
28 }
29 
30 inline void read()
31 {
32     for(int i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y);
33 }
34 
35 inline void go()
36 {
37     int ans=0;
38     sort(p+1,p+1+n,cmpx);
39     for(int i=1;i<=n;i+=2) ans+=p[i+1].y-p[i].y;
40     sort(p+1,p+1+n,cmpy);
41     for(int i=1;i<=n;i+=2) ans+=p[i+1].x-p[i].x;
42     printf("The length of the fence will be %d units.\n",ans);
43 }
44 
45 int main()
46 {
47     while(scanf("%d",&n),n) read(),go();
48     return 0;
49 }

 

 

 

 

posted @ 2013-02-24 20:03  proverbs  阅读(176)  评论(0编辑  收藏  举报