Description
Advanced Cargo Movement, Ltd. is successfully expanding. in order to meet new demands on truck maintenance, the management of the company decided to build a new truck depot. A suitable lot for building a depot was purchased and a construction company ''Masonry and Fences for Future, Ltd. ''was hired to build a depot.
The area of the depot will be enclosed by a fence. the fence is supposed to enclose a connected area of a lot and each part of the fence follows North-South or East-West ction. at each place where the fence changes its direction, there is a single post. the posts are only at points where the fence changes the direction, I. e ., there are no unnecessary posts. when MFF workers have built all of the posts, they lost the plan of a depot being built. at this moment, they asked you for a help.
Given the coordinates of all the posts, your task is to compute the length of the fence.
Input
The input consists of several blocks. the first line of each block contains a single number P, 1 <= P <= 100 000. P is the number of posts which have been built. each of the following P lines contains two integers X and Y, 0 <= X, Y <= 10 000, which represent coordinates of the posts in MFF internal units (which no one else is able to understand ). no two posts have the same coordinates.
Each block is followed by a single empty line and the input is terminated by a line containing a single number 0.
Output
Output contains a single line for each block. the line shoshould contain the text "The length of the fence will be L units. ", where L is replaced by an actual length of the fence. you can assume that the fence can always be built.
Sample Input
61 11 33 32 13 22 20
Sample Output
The length of the fence will be 8 units.
The number is the coordinate of the column. No two columns have a fence between them to find the length of the fence...
Solution: sort the values by X for the first time, and sort the values by y for the second time respectively.
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 (. x = B. x) 12 return. y <B. y; 13 return. x <B. x; 14} 15 bool cmp2 (weizhi a, weizhi B) 16 {17 if (. y = B. y) 18 return. x <B. x; 19 return. 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", & 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}
View Code
POJ 1788 Building a New Depot solution report