poj1259The Picnic & hdu6219 Empty Convex Polygon(17沈陽區域賽C)【最大空凸包】
阿新 • • 發佈:2018-10-14
pty sin ios 面積 image .cn names 鏈接 turn
題目鏈接:
http://acm.hdu.edu.cn/showproblem.php?pid=6219
http://poj.org/problem?id=1259
一份代碼A兩題。
題意:
給n個點,求一個面積最大的空凸包。
思路:
空凸包,就是一個內部沒有其他給定點的凸包。
詳細講解見:https://blog.csdn.net/nyroro/article/details/45268767
總的來說就是先枚舉凸包的最左下角的點O。按照極坐標排序。
dp[i][j]表示組成凸包的最後一個三角形的是Oij時的最大面積。dp[i][j]=max(dp[i][j],triangle(O,i,j)+dp[j][k])
再枚舉凸包上最後的一個點i,枚舉所有比i小的合法的j。
具體講解見:https://blog.csdn.net/cdsszjj/article/details/79366813
復雜度O(n^3)
1 #include<iostream> 2 #include<cmath> 3 #include<algorithm> 4 #include<stdio.h> 5 #include<cstring> 6 #include<vector> 7 #include<map> 8 #include<set> 9 10 #define inf 0x3f3f3f3f 11using namespace std; 12 typedef long long LL; 13 14 const int maxn = 105; 15 struct point{ 16 double x, y; 17 point(){} 18 point(double _x, double _y):x(_x), y(_y){} 19 point operator + (const point &b) const{return point(x + b.x, y + b.y);} 20 point operator - (constpoint &b) const{return point(x - b.x, y - b.y);} 21 double operator * (const point &b) const {return x * b.y - y * b.x;} 22 double len() const {return x * x + y * y;} 23 /*int operator < (const point &a) const 24 { 25 if((*this)*a > 0 || (*this) *a == 0 && len() < a.len()) 26 return 1; 27 return 0; 28 }*/ 29 30 }a[maxn], p[maxn], yuan; 31 /*bool cmp(const point &a, const point &b) 32 { 33 int c = a * b; 34 if(c == 0)return a.len() < b.len(); 35 return c > 0; 36 }*/ 37 double dp[maxn][maxn], ans; 38 int t, n, m; 39 bool cmp(const point &a, const point &b) 40 { 41 int res = (a - yuan) * (b - yuan); 42 if(res)return res > 0; 43 return (a - yuan).len() < (b - yuan).len(); 44 } 45 void solve() 46 { 47 memset(dp, 0, sizeof(dp)); 48 sort(p + 1, p + m + 1, cmp); 49 for(int i = 1; i <= m; i++){ 50 int j = i - 1; 51 while(j && !((p[i] - yuan) * (p[j] - yuan)))j--; 52 bool bz = (j == i - 1); 53 while(j){ 54 int k = j - 1; 55 while(k && (p[i] - p[k]) * (p[j] - p[k]) > 0)k--; 56 double area = fabs((p[i] - yuan) * (p[j] - yuan)) / 2; 57 if(k) area += dp[j][k]; 58 if(bz) dp[i][j] = area; 59 ans = max(ans, area); 60 j = k; 61 } 62 if(bz){ 63 for(int j = 1; j < i; j++){ 64 dp[i][j] = max(dp[i][j], dp[i][j - 1]); 65 } 66 } 67 } 68 } 69 70 int getint() 71 { 72 int i = 0, f = 1; 73 char c; 74 for(c = getchar(); (c != ‘-‘) && (c < ‘0‘ || c > ‘9‘); c = getchar()); 75 if(c == ‘-‘)f = -1, c = getchar(); 76 for(;c >= ‘0‘ && c <= ‘9‘; c = getchar())i = (i << 3) + (i << 1) + c - ‘0‘; 77 return i * f; 78 } 79 80 int main(){ 81 82 //scanf("%d", &t); 83 t = getint(); 84 while(t--){ 85 //scanf("%d", &n); 86 n = getint(); 87 ans = 0; 88 for(int i = 1; i <= n; i++){ 89 cin>>a[i].x>>a[i].y; 90 } 91 for(int i = 1; i <= n; i++){ 92 yuan = a[i]; 93 m = 0; 94 for(int j = 1; j <= n; j++){ 95 if(a[j].y > a[i].y || a[j].y == a[i].y && a[j].x > a[i].x) 96 p[++m] = a[j];//只取右上角的點 97 } 98 solve(); 99 100 } 101 printf("%0.1f\n", ans); 102 } 103 return 0; 104 }
poj1259The Picnic & hdu6219 Empty Convex Polygon(17沈陽區域賽C)【最大空凸包】