http://acm.nyist.net/JudgeOnline/problem.php?pid=78
Lap Pool time limit:MS | Memory limit:65535 KB Difficulty:4
-
Describe
-
There is a pasture, where there are many water supplies, and now the owner of the ranch wants to circle the water supply with a fence to prevent them from drinking water, the pools are labeled with their coordinates, and now you need to write a program that uses the shortest fence to circle the water supply units! (The fence is large enough and variable in length)
-
Input
-
The first line of input is N, which represents the test data in N Group (1<=n<=10)
The second line is M, which represents a total of M water supply units (3<=M<=100) for this group of test data.
The next M-line represents the horizontal ordinate of each water supply unit.
-
Output
-
Output each fence through the coordinate points of each water supply device, and according to the x-axis coordinate value from small to large output, if the x-axis coordinate value is the same, and then Yasuteru y-coordinate value from small to large output
-
Sample input
-
140 01 12) 33 0
-
Sample output
-
0 02 33 0
Analysis:
Convex package.
AC Code:
1 //Nyoj 78--Circle Pool Andrew convex packet Algorithm O (NLOGN)2 //outputs the vertices of the convex hull in the order from small to large x after Y3 //4 /*test Data5 96 $7 the8 the9 theTen the One the A $ - the - $ the = = 1628 - - */ - +#include <cstdio> -#include <cmath> +#include <algorithm> A using namespacestd; at - Const intMAXP =1010; - Const DoublePzero =0.00001; - - structpoint{ - intx, y; in}p[maxp],chp[maxp*2]; - to DoubleXJ (Point A, point B, point C) { + //Vector a->b (b.x-a.x, B.Y-A.Y) - //Vector a->c (c.x-a.x, C.Y-A.Y) the return(b.x-a.x) * (C.Y-A.Y)-(c.x-a.x) * (b.y-a.y); * } $ Panax Notoginseng BOOLComp (Point A,point b) { - return(a.x < b.x) | | ((a.x==b.x) && (a.y<b.y)); the } + A intAndrewconvexhull (Point *CHP,intN) { the //calculate convex hull chp[] counter-clockwise save Point +Sort (p, p +N, comp); - intNCHP =0; $ for(inti =0; I < n; chp[nchp++] = p[i++]) $ while(NCHP >=2&& XJ (chp[nchp-2], chp[nchp-1], P[i]) <=0) -nchp--; - for(inti = n-1, M = nchp +1; I >=0; chp[nchp++] = p[i--]) the while(NCHP >= m && XJ (chp[nchp-2], chp[nchp-1], P[i]) <=0) -nchp--;Wuyi return--NCHP; the } - Wu voidInitintN) { - //input About for(inti =0; I < n; i++) $scanf"%d%d", &p[i].x, &p[i].y); - } - - voidOutput (intNCHP) { ASort (CHP, CHP +NCHP, comp); + for(inti =0; i < NCHP; i++){ theprintf"%d%d\n", chp[i].x, chp[i].y); - } $ } the the intMain () the { the //freopen ("In.txt", "R", stdin); - intN;SCANF ("%d",&n); in while(~SCANF ("%d",&N)) { the the init (n); About intNCHP =Andrewconvexhull (CHp, n); the Output (NCHP); the } the return 0; +}
Nyist 78 Laps Pool