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
The following: the use of the characteristics of the difference, if the difference is negative is a clockwise turn, if it is counterclockwise, if the 0 is a common line;
Code:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6 using namespacestd;7 structnode{8 intx, y;9Friendint operator<(Node A,node b) {Ten if(a.x<b.x| | (A.X==B.X&&A.Y<B.Y))return 1; One Else return 0; A } - }; - intChaji (Node a,node b,node c) { the return(b.x-a.x) * (C.Y-A.Y)-(B.Y-A.Y) * (c.x-a.x); - } -Node a[ the],ans[ the]; - intMain () { + intn,m; -scanf"%d",&N); + while(n--){ Ascanf"%d",&m); at for(intI=0; i<m;i++) scanf ("%d%d",&a[i].x,&a[i].y); -Sort (a,a+m); - intk=0; - for(intI=0; i<m;i++){ - while(k>1&&chaji (ans[k-2],ans[k-1],a[i]) <=0) k--; -ans[k++]=A[i]; in } - intt=K; to for(intj=n-1; i>=0; i--){ + while(K>t&&chaji (ans[k-2],ans[k-1],a[i]) <=0) k--; -ans[k++]=A[i]; the } *k--; $Sort (ans,ans+k);Panax Notoginseng for(intI=0; i<k;i++) printf ("%d%d\n", ans[i].x,ans[i].y); - } the return 0; +}
Lap Pool (Introduction to convex hull)