USACO Section 5.1 Fencing the Cows
2014-05-25 18:36
375 查看
题意:
凸包
思路:
凸包 - -b (呵呵…)
代码:
/*
ID: housera1
PROG: fc
LANG: C++
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define N 10010
struct point
{
double x,y;
}lis
;
int yes
,top,n;
double cross(point p0,point p1,point p2)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
double dis(point p1,point p2)
{
return sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}
bool cmp(point p1,point p2)
{
double tmp=cross(lis[0],p1,p2);
if((fabs(tmp)<1e-8&&dis(lis[0],p1)<dis(lis[0],p2))||tmp>1e-8) return true;
return false;
}
void init()
{
int i,k;
point p0;
scanf("%lf%lf",&lis[0].x,&lis[0].y);
p0=lis[0];
k=0;
for(i=1;i<n;i++)
{
scanf("%lf%lf",&lis[i].x,&lis[i].y);
if( (p0.y>lis[i].y) || (fabs(p0.y-lis[i].y)<1e-8&&(p0.x>lis[i].x)) )
{
p0.x=lis[i].x;
p0.y=lis[i].y;
k=i;
}
}
lis[k]=lis[0];
lis[0]=p0;
sort(lis+1,lis+n,cmp);
}
void graham()
{
int i;
if(n==1) {top=0;yes[0]=0;}
if(n==2)
{
top=1;
yes[0]=0;
yes[1]=1;
}
if(n>2)
{
for(i=0;i<=1;i++) yes[i]=i;
top=1;
for(i=2;i<n;i++)
{
while(top>0&&cross(lis[yes[top-1]],lis[yes[top]],lis[i])<=0) top--;
top++;
yes[top]=i;
}
}
}
double solve()
{
double res=0;
int i;
if(n==1) return 0;
else if(n==2) return dis(lis[0],lis[1]);
else
{
for(i=0;i<=top;i++) res+=dis(lis[yes[i]],lis[yes[(i+1)%(top+1)]]);
return res;
}
}
int main()
{
int Debug=0;
if(!Debug)
{
freopen("fc.in","r",stdin);
freopen("fc.out","w",stdout);
}
scanf("%d",&n);
init();
graham();
printf("%.2f\n",solve());
return 0;
}
凸包
思路:
凸包 - -b (呵呵…)
代码:
/*
ID: housera1
PROG: fc
LANG: C++
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define N 10010
struct point
{
double x,y;
}lis
;
int yes
,top,n;
double cross(point p0,point p1,point p2)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
double dis(point p1,point p2)
{
return sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}
bool cmp(point p1,point p2)
{
double tmp=cross(lis[0],p1,p2);
if((fabs(tmp)<1e-8&&dis(lis[0],p1)<dis(lis[0],p2))||tmp>1e-8) return true;
return false;
}
void init()
{
int i,k;
point p0;
scanf("%lf%lf",&lis[0].x,&lis[0].y);
p0=lis[0];
k=0;
for(i=1;i<n;i++)
{
scanf("%lf%lf",&lis[i].x,&lis[i].y);
if( (p0.y>lis[i].y) || (fabs(p0.y-lis[i].y)<1e-8&&(p0.x>lis[i].x)) )
{
p0.x=lis[i].x;
p0.y=lis[i].y;
k=i;
}
}
lis[k]=lis[0];
lis[0]=p0;
sort(lis+1,lis+n,cmp);
}
void graham()
{
int i;
if(n==1) {top=0;yes[0]=0;}
if(n==2)
{
top=1;
yes[0]=0;
yes[1]=1;
}
if(n>2)
{
for(i=0;i<=1;i++) yes[i]=i;
top=1;
for(i=2;i<n;i++)
{
while(top>0&&cross(lis[yes[top-1]],lis[yes[top]],lis[i])<=0) top--;
top++;
yes[top]=i;
}
}
}
double solve()
{
double res=0;
int i;
if(n==1) return 0;
else if(n==2) return dis(lis[0],lis[1]);
else
{
for(i=0;i<=top;i++) res+=dis(lis[yes[i]],lis[yes[(i+1)%(top+1)]]);
return res;
}
}
int main()
{
int Debug=0;
if(!Debug)
{
freopen("fc.in","r",stdin);
freopen("fc.out","w",stdout);
}
scanf("%d",&n);
init();
graham();
printf("%.2f\n",solve());
return 0;
}
相关文章推荐
- 洛谷 P2742 [USACO5.1]圈奶牛Fencing the Cows
- usaco 5.1 Fencing the Cows(凸包模板题)
- 凸包——Luogu2742 [USACO5.1]圈奶牛Fencing the Cows
- P2742 [USACO5.1]圈奶牛Fencing the Cows
- P2742 [USACO5.1]圈奶牛Fencing the Cows
- USACO Section 5.1 Fencing the Cows - 凸包模板题~~
- USACO Section 5.1 Fencing the Cows(凸包)
- 太感动了!第一次自己打出了USACO的题~ [USACO5.1]圈奶牛Fencing the Cows
- [USACO5.1]Fencing the Cows
- Section 5.1 Fencing the Cows
- USACO 5.1 Fencing the Cows(凸包)
- Fencing the Cows_usaco 5.1_凸包
- USACO 5.1 Fencing the Cows
- USACO 5.1 fencing the cows——计算几何/凸包模板
- [USACO5.1]圈奶牛Fencing the Cows(凸包模板)
- [luogu2742]:[USACO5.1]圈奶牛Fencing the Cows
- usaco fencing the cows
- USACO5.1.1 Fencing the Cows (fc)
- 计算几何 usaco Fencing the Cows 圈奶牛
- C++&Pascal——【USACO 5.1.1】——Fencing the Cows