国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

POJ3348-凸包

2019-11-14 09:33:31
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

題意:一片草地上有n課樹(shù),現(xiàn)在你想用繩子圈出一個(gè)盡可能大的面積出來(lái)養(yǎng)牛。已知每只牛需要50單位的面積,問(wèn)最多能養(yǎng)幾只牛。

1.按極角排序。

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>using namespace std;int n,stack[10010],top;const int maxn = 1e6+10;const double eps = 1e-8;struct Tpoint{ double x; double y;}list[10010];;int dblcmp(double p){ if(fabs(p)<eps) return 0; return p>0?1:-1;}double dist(Tpoint a, Tpoint b){ return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));}double Cross(Tpoint p0, Tpoint p1, Tpoint p2) { return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);}bool cmp(Tpoint p1, Tpoint p2){ double temp = Cross(list[0],p1,p2); int tt = dblcmp(temp); if(!tt) return dist(list[0],p1) < dist (list[0],p2); return tt>0;}void Graham(){ Tpoint p0 = list[0]; int k = 0; for(int i=1;i<n;i++){ if(p0.y>list[i].y || (p0.y==list[i].y && p0.x>list[i].x)){ p0 = list[i]; k = i; } } swap(list[k], list[0]); sort(list+1, list+n, cmp); stack[0] = 0; stack[1] = 1; top = 1; for(int i=2;i<n;i++){ while( dblcmp(Cross(list[stack[top-1]], list[stack[top]],list[i])<0)){ top--; } stack[++top] = i; }}void init(){ for(int i=0 ; i < n ; i++ ) scanf("%lf%lf",&list[i].x,&list[i].y); memset(stack,0,sizeof(stack));}void sov(){ double area = 0; for (int i = 0; i <= top; i++) area +=fabs(Cross(list[stack[(i+1)%(top+1)]],list[stack[i]],list[stack[0]])); PRintf ("%d/n", (int)area/100);}int main(){ while(~scanf("%d",&n)){ if(n == 1||n == 2) { printf("0/n");continue;} init(); Graham(); sov(); }}

2.按x排序。(x相同按y排序)

從小到大遍歷,走到最大是一半的凸包,然后從大到小在走一遍,另半個(gè)凸包

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>using namespace std;int n,stack[10010],top;const int maxn = 1e6+10;const double eps = 1e-8;struct Tpoint{ double x; double y;}list[10010];;int dblcmp(double p){ if(fabs(p)<eps) return 0; return p>0?1:-1;}double Cross(Tpoint p0, Tpoint p1, Tpoint p2) { return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);}bool cmp(Tpoint p1, Tpoint p2){ if(p1.y == p2.y) return p1.x < p2.x; return p1.y < p2.y;}void Graham(){ top = 1; for(int i = 0 ; i <= 2; i++) stack[i] = i; for(int i=2;i<n;i++){ while(top && dblcmp(Cross(list[stack[top-1]], list[stack[top]],list[i])<0)){ top--; } stack[++top] = i; } int len = top; stack[++top] = n-2; for(int i = n-3; i >= 0 ; i--){ while(top!=len && dblcmp(Cross(list[stack[top-1]], list[stack[top]],list[i])<0)) top--; stack[++top] = i; }}void init(){ for(int i=0 ; i < n ; i++ ) scanf("%lf%lf",&list[i].x,&list[i].y); memset(stack,0,sizeof(stack)); sort(list, list+n, cmp);}void sov(){ double area = 0; for (int i = 0; i <= top; i++) area +=fabs(Cross(list[stack[(i+1)%(top+1)]],list[stack[i]],list[stack[0]])); printf ("%d/n", (int)area/100);}int main(){ while(~scanf("%d",&n)){ if(n == 1||n == 2) { printf("0/n");continue;} init(); Graham(); sov(); }}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 水富县| 华亭县| 北宁市| 彰武县| 吉木乃县| 凤庆县| 铁力市| 郴州市| 岑巩县| 永德县| 攀枝花市| 双柏县| 永仁县| 余江县| 稷山县| 同德县| 仙桃市| 哈尔滨市| 安平县| 青铜峡市| 鱼台县| 宁晋县| 株洲县| 永兴县| 宁陕县| 贵德县| 定襄县| 朔州市| 弥勒县| 城步| 乐山市| 沁阳市| 北安市| 喀喇| 修武县| 卢氏县| 丹寨县| 信阳市| 屯昌县| 泾源县| 九寨沟县|