poj1113 Wall
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define pi acos(-1.0)
struct point
{
double x;
double y;
}pp[1010];
struct Vector
{
double x;
double y;
};
double chaji(Vector a,Vector b)//a*b
{
return a.x*b.y-b.x*a.y;
}
Vector operator-(point a,point b)
{
Vector temp;
temp.x=a.x-b.x;
temp.y=a.y-b.y;
return temp;
}
bool cmp(const point a,const point b)
{
Vector m=a-pp[0];
Vector n=b-pp[0];
if(chaji(m,n)>0)
{
return true;
}
if(chaji(m,n)==0)
{
if(a.x==b.x)
{
return a.y<b.y;
}
return a.x<b.x;
}
return false;
}
struct node
{
int u;//节点u
node *next;
node *front;
};
double distpp(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
int N,L;
scanf("%d %d",&N,&L);
int i;
for(i=0;i<N;i++)
{
scanf("%lf %lf",&pp[i].x,&pp[i].y);
}
int mini;
mini=0;
for(i=1;i<N;i++)
{
if(pp[i].y<pp[mini].y)
{
mini=i;
}
if(pp[i].y==pp[mini].y)
{
if(pp[i].x<pp[mini].x)
{
mini=i;
}
}
}
point temp;
temp=pp[0];
pp[0]=pp[mini];
pp[mini]=temp;
//pp[0]为基点
sort(pp+1,pp+N,cmp);
node head;
head.front=NULL;
head.next=NULL;
head.u=0;
head.next=new node;
head.next->front=&head;
head.next->u=1;
node *p;
p=head.next;
p->next=NULL;
Vector m,n;
for(i=2;i<N;i++)//p指针一直指向链表的最后的一个节点
{
m=pp[p->u]-pp[p->front->u];
n=pp[i]-pp[p->u];
if(chaji(m,n)>=0)
{
p->next=new node;
p->next->next=NULL;
p->next->front=p;
p->next->u=i;
p=p->next;
continue;
}
bool flag=false;
int v=i;
if(chaji(m,n)<0)//v为处理中的当前节点
{
//node linshi;
p->next=new node;
p->next->next=NULL;
p->next->front=p->front;
p->next->u=v;
p->front->next=p->next;
p=p->front;
flag=true;
if(p->front==NULL)
{
while(p->next!=NULL)
{
p=p->next;
}
continue;
}
m=pp[p->u]-pp[p->front->u];
n=pp[p->next->u]-pp[p->u];
}
if(flag)
{
while(chaji(m,n)<=0)
{
p->next->front=p->front;
p->front->next=p->next;
p=p->front;
if(p->front==NULL)
{
break;
}
m=pp[p->u]-pp[p->front->u];
n=pp[p->next->u]-pp[p->u];
}
}
while(p->next!=NULL)
{
p=p->next;
}
}
//只差最后的结果计算
p=&head;
double res=0;
while(p->next!=NULL)
{
res+=distpp(pp[p->u],pp[p->next->u]);
p=p->next;
}
res+=distpp(pp[0],pp[p->u]);
res+=2*pi*L;
int grid=(int)(res*10)%10;
if(grid>=5)
{
printf("%d\n",1+(int)res);
}
else
{
printf("%d\n",(int)res);
}
return 0;
}
//264K 47MS
我是用链表做的。。。

浙公网安备 33010602011771号