poj1228
题目大意:给定一些点,问存不存在稳定凸包。。所谓的稳定凸包,就是每条凸包边上至少3个点。。
思路:凸包,然后判断是否有其他点在边上。。一般来说凸包算法是不含共线情况的,况且边不多,所以只能这样判断了。。
code:
1 /* 2 Time:2013-04-06 11:55:37 3 State:Accepted 4 5 */ 6 7 #include<iostream> 8 #include<fstream> 9 #include<cstring> 10 #include<cstdlib> 11 #include<cstdio> 12 #include<string> 13 #include<cmath> 14 #include<algorithm> 15 struct oo{int x , y; }; 16 using namespace std; 17 int T, n, px, py ,bo[2000]; 18 oo a[2000],q[2000]; 19 20 void init(){ 21 scanf("%d",&n); 22 memset(a , 0 ,sizeof(a)); 23 memset(q , 0 ,sizeof(q)); 24 memset(bo ,0 ,sizeof(bo)); 25 for (int i = 1; i <= n ;++i) 26 scanf("%d%d",&a[i].x, &a[i].y); 27 28 } 29 30 bool cmp(const oo a , const oo b){ 31 if (a.y < b.y) return true; 32 if (a.y == b.y && a.x < b.x) return true; 33 return false; 34 35 } 36 37 bool cmpx(const oo a, const oo b){ 38 int x1 = a.x - px; 39 int x2 = b.x - px; 40 int y1 = a.y - py; 41 int y2 = b.y - py; 42 if (x1*y2-x2*y1 > 0) return true; 43 if (x1*y2 == x2*y1 && x1*x1 + y1*y1 < x2*x2 + y2*y2) return true; 44 return false; 45 } 46 void solve(){ 47 if (n < 6){ 48 printf("NO\n"); 49 return; 50 } 51 sort(a + 1, a + 1 + n, cmp); 52 px = a[1].x; 53 py = a[1].y; 54 if (n > 2) sort(a + 2, a + 1 + n, cmpx); 55 int h = 1, t = 2; 56 q[1] = a[1]; 57 q[2] = a[2]; 58 int x1, y1, x2, y2; 59 for (int i = 3; i <= n; ++i){ 60 while (true){ 61 if (t == 1) break; 62 x1 = a[i].x - q[t - 1].x; 63 x2 = q[t].x - q[t - 1].x; 64 y1 = a[i].y - q[t - 1].y; 65 y2 = q[t].y - q[t - 1].y; 66 if (x1*y2 - x2*y1 >= 0) --t; 67 else break; 68 } 69 q[++t] = a[i]; 70 } 71 72 if (t < 3){ 73 printf("NO\n"); 74 return; 75 } 76 77 q[t + 1] = q[1]; 78 int cnt = 0; 79 80 for (int i = 1; i <= t; ++i){ 81 for (int j = 1; j <= n ; ++j) 82 if ( (a[j].x!=q[i].x || a[j].y != q[i].y) 83 && (a[j].x != q[i + 1].x || a[j].y != q[i + 1].y)){ 84 if ((q[i].x - a[j].x)*(q[i + 1].y - a[j].y) != 85 (q[i + 1].x - a[j].x)*(q[i].y - a[j].y)) continue; 86 ++cnt; 87 break; 88 } 89 } 90 if (cnt >= t) printf("YES\n"); 91 else printf("NO\n"); 92 93 } 94 95 int main(){ 96 freopen("poj1228.in","r",stdin); 97 freopen("poj1228.out","w",stdout); 98 scanf("%d",&T); 99 for (int i = 1; i <= T; ++i){ 100 init(); 101 solve(); 102 } 103 fclose(stdin); fclose(stdout); 104 }