标签:name sub style amp 表示 opera 最大 width bsp
题目链接:
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 11 using 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 - (const point &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)【最大空凸包】
标签:name sub style amp 表示 opera 最大 width bsp
原文地址:https://www.cnblogs.com/wyboooo/p/9785652.html