您的位置:首页 > 其它

POJ 3348 Cows (凸包模板)

2018-03-28 15:03 387 查看
传送门:点击打开链接

题目大意:草地上有些树,用树做篱笆围一块最大的面积来养牛,每头牛要50平方米才能养活,问最多能养多少只羊
解法:凸包求面积,分解成三角形用叉积求面积。
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int N = 1e4+10;
int n;

struct pot{
double x,y;
}p
;

bool cmp1(pot a,pot b){
if(a.y==b.y) return a.x<b.x;
return a.y<b.y;
}

double dist(pot a,pot b){
return (b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y);
}

double mul(pot a,pot b,pot c){
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}

bool cmp2(pot a,pot b){
double tp=mul(p[0],a,b);
if(tp==0) return dist(p[0],a)<dist(p[0],b);
return tp>0;
}

int main(){
while(cin>>n){
for(int i=0;i<n;i++) {
double a,b;
cin>>a>>b;
p[i].x=a,p[i].y=b;
}
sort(p,p+n,cmp1);
sort(p+1,p+n,cmp2);
int stk
,top=2;
stk[0]=0,stk[1]=1;
for(int i=2;i<n;i++){
while(mul(p[stk[top-2]],p[i],p[stk[top-1]])>=0)
top--;
stk[top++]=i;
}
double s=0;
for(int i=1;i<top-1;i++)
s+=mul(p[0],p[stk[i]],p[stk[i+1]]);
cout<<(int)(s/100)<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: