poj1259The Picnic & hdu6219 Empty Convex Polygon(17瀋陽地區賽C)【最大空凸包】

來源:互聯網
上載者:User

標籤:name   sub   style   amp   表示   opera   最大   width   bsp   

題目連結:

http://acm.hdu.edu.cn/showproblem.php?pid=6219

http://poj.org/problem?id=1259

一份代碼A兩題。

題意:

給n個點,求一個面積最大的空凸包。

思路:

空凸包,就是一個內部沒有其他給定點的凸包。

詳細講解見:45268767

總的來說就是先枚舉凸包的最左下角的點O。按照極座標排序。

dp[i][j]表示組成凸包的最後一個三角形的是Oij時的最大面積。dp[i][j]=max(dp[i][j],triangle(O,i,j)+dp[j][k])

再枚舉凸包上最後的一個點i,枚舉所有比i小的合法的j。

具體講解見: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)【最大空凸包】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.