标签:连线 sort none fine amp max 代码 iostream alt
【POJ 1696】 Space Ants
【题目大意】
给定多个点,对他们按照下面的规则排序,每个都在前一个点组成的左边,并且连线不相交(典型如图)
【题目分析】
不断进行极角排序,不断选取一定区域内最符合要求的解
【代码】
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #define ll double 5 using namespace std; 6 const double eps = 1e-9; 7 const int MAXN = 1010; 8 int sgn(ll x) 9 { 10 if (x > eps) return 1; 11 if (x < -eps)return -1; 12 return 0; 13 } 14 struct P 15 { 16 ll x, y ; 17 int index; 18 P() {}; 19 P(double a, double b) :x(a), y(b) {} 20 P operator -(P a) { return P(x - a.x, y - a.y); } 21 double operator *(P a) { return x * a.x + y * a.y ; } 22 double operator ^(P a) { return x * a.y - y * a.x ; } 23 }; 24 struct L 25 { 26 P s, t; 27 L() {} 28 L(P a, P b) :s(a), t(b) {} 29 }; 30 double dis(P a, P b) 31 { 32 return (b - a)*(b - a); 33 } 34 P p[MAXN]; 35 int pos; 36 bool cmp(P a, P b) 37 { 38 int tmp = sgn((a - p[pos]) ^ (b - p[pos])); 39 if (tmp == 0) return dis(b, p[pos]) > dis(a, p[pos]); 40 if (tmp < 0) return false; 41 return true; 42 } 43 int main() 44 { 45 int T; 46 scanf("%d", &T); 47 while (T--) 48 { 49 int n; 50 scanf("%d", &n); 51 for (int i = 1; i <= n; i++) 52 { 53 scanf("%d %lf %lf", &p[i].index, &p[i].x, &p[i].y); 54 if ((p[i].y == p[1].y && p[i].x < p[1].x )||(p[i].y<p[1].y)) 55 swap(p[1], p[i]); 56 } 57 pos = 1; 58 for (int i = 2; i <= n; i++) 59 { 60 sort(p+i,p+n+1,cmp); 61 pos++; 62 } 63 printf("%d ", n); 64 for (int i = 1; i <= n; i++) 65 { 66 printf("%d", p[i].index); 67 if (i != n) 68 printf(" "); 69 else 70 printf("\n"); 71 } 72 } 73 return 0; 74 }
标签:连线 sort none fine amp max 代码 iostream alt
原文地址:https://www.cnblogs.com/rentu/p/11631727.html