poj1113 Wall

Wall
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 39929   Accepted: 13613

Description

Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he would not listen to his Architect's proposals to build a beautiful brick wall with a perfect shape and nice tall towers. Instead, he ordered to build the wall around the whole castle using the least amount of stone and labor, but demanded that the wall should not come closer to the castle than a certain distance. If the King finds that the Architect has used more resources to build the wall than it was absolutely necessary to satisfy those requirements, then the Architect will loose his head. Moreover, he demanded Architect to introduce at once a plan of the wall listing the exact amount of resources that are needed to build the wall. 

Your task is to help poor Architect to save his head, by writing a program that will find the minimum possible length of the wall that he could build around the castle to satisfy King's requirements. 

The task is somewhat simplified by the fact, that the King's castle has a polygonal shape and is situated on a flat ground. The Architect has already established a Cartesian coordinate system and has precisely measured the coordinates of all castle's vertices in feet.

Input

The first line of the input file contains two integer numbers N and L separated by a space. N (3 <= N <= 1000) is the number of vertices in the King's castle, and L (1 <= L <= 1000) is the minimal number of feet that King allows for the wall to come close to the castle. 

Next N lines describe coordinates of castle's vertices in a clockwise order. Each line contains two integer numbers Xi and Yi separated by a space (-10000 <= Xi, Yi <= 10000) that represent the coordinates of ith vertex. All vertices are different and the sides of the castle do not intersect anywhere except for vertices.

Output

Write to the output file the single number that represents the minimal possible length of the wall in feet that could be built around the castle to satisfy King's requirements. You must present the integer number of feet to the King, because the floating numbers are not invented yet. However, you must round the result in such a way, that it is accurate to 8 inches (1 foot is equal to 12 inches), since the King will not tolerate larger error in the estimates.

Sample Input

9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200

Sample Output

1628

Hint

结果四舍五入就可以了

Source

Northeastern Europe 2001


题解:

我好强哦到现在还没过这题。

裸凸包即可。


代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
const double pi=3.1415926;
int n,top;
double L;
struct node{
	double x,y;
}P[1005],stack[1005];
double cross(node &a,node &b,node &c){
	return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}
double dis(node &a,node &b){
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void swap(node &a,node &b){
	double t=a.x; a.x=b.x; b.x=t;
	t=a.y; a.y=b.y; b.y=t;
}
bool cmp(node &a,node &b){
	double d=cross(a,b,P[1]);
	if (d>0) return 1;
	if (fabs(d)<1e-8 && dis(a,P[1])<dis(b,P[1])) return 1;
	return 0;
}
void push(node a){
	top++;
	stack[top].x=a.x;
	stack[top].y=a.y;
}
void Tubao(){
	top=0;
	push(P[1]);
	push(P[2]);
	for (int i=3;i<=n;i++){
		while (top>=2 && cross(P[i],stack[top],stack[top-1])>=0) top--;
		push(P[i]);
	}
}
int main(){
	freopen("1.in","r",stdin);
	scanf("%d%lf",&n,&L);
	for (int i=1;i<=n;i++) scanf("%lf%lf",&P[i].x,&P[i].y);
	int p=1;
	for (int i=2;i<=n;i++)
	if (P[i].y<P[p].y || P[i].y==P[p].y && P[i].x<P[p].x)
		p=i;
	if (p!=1) swap(P[1],P[p]);
	std::sort(P+2,P+n+1,cmp);
	Tubao();
	double ans=0;
	for (int i=1;i<top;i++) ans+=dis(stack[i],stack[i+1]);
	ans+=dis(stack[top],stack[1]);
	printf("%.0lf\n",ans+2*L*pi);
	return 0;
}
 
 

另附我的代码(错的)

#include <iostream>  
#include <fstream>  
#include <cstdio>  
#include <cmath>  
#include <map>  
#include <set>  
#include <bitset>  
#include <ctime>  
#include <cstring>  
#include <algorithm>  
#include <stack>  
#include <queue>  
#include <vector>  
#include <list> 
using namespace std;
struct aaa{
	double l,r;
}a[100001];
int n,l,r,flag,i,tot,d[100001];
double ans,L;
double cha(aaa a,aaa b,aaa c){
	return(b.l-a.l)*(c.r-a.r)-(c.l-a.l)*(b.r-a.r);
}
double dis(aaa a,aaa b){
	return sqrt((b.l-a.l)*(b.l-a.l)+(b.r-a.r)*(b.r-a.r));
}
bool cmp(aaa aa,aaa b){
	double t=cha(a[1],aa,b);
	//printf("%d %d %d\n",t,a[1].l,a[1].r);
	return (t>0||(fabs(t)<1e-8&&dis(a[1],aa)<dis(a[1],b)));
}
int main(){
	scanf("%d%lf",&n,&L);
	for(i=1;i<=n;i++)scanf("%lf%lf",&a[i].l,&a[i].r);
	for(i=1;i<=n;i++){
		if((!flag)||(a[i].l<l)||(a[i].l==l&&a[i].r<r)){
		flag=i;l=a[i].l;r=a[i].r;
	}
	}
	//printf("%d\n",flag);
	a[0]=a[1];
	a[1]=a[flag];
	a[flag]=a[0];
	sort(a+2,a+n+1,cmp);
	for(i=1;i<=n;i++)printf("%.2lf %.2lf\n",a[i].l,a[i].r);
	tot=2;
	d[1]=1;d[2]=2;
	for(i=3;i<=n;i++){
		printf("%d %d %d %d\n",i,tot,d[tot-1],d[tot]);
		while(tot>=2&&cha(a[d[tot-1]],a[d[tot]],a[i])<=0)tot--;
		d[++tot]=i;
	}
	d[tot+1]=1;
	for(i=1;i<=tot;i++)printf("%d ",d[i]);
	puts("");
	ans=0;
	for(i=1;i<=tot;i++){
	ans+=dis(a[d[i]],a[d[i+1]]);
	printf("%.2lf\n",ans);
	}
	ans+=L*2*3.1415926535;
	printf("%.0lf",ans);
}



凸包模板:

#include<stdio.h>
   #include<stdlib.h>
   #include<math.h>
   typedef struct 
   {
           double x , y ;
   }POINT ;
   POINT result[110] ;// 模拟堆栈S,保存凸包上的点
   POINT tree[110] ;
  int n , top ;
  double Distance ( POINT p1 , POINT p2 ) 
  {
         return sqrt( (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y) ) ;
  }
  double Multiply(POINT p1 , POINT p2 , POINT p3) // 叉积 
  {
         return ( (p2.x - p1.x)*(p3.y - p1.y) - (p2.y - p1.y)*(p3.x - p1.x) ) ;
  }
  int cmp ( const void *p1 , const void *p2 )
  {
      POINT *p3,*p4;
      double m;
      p3 = (POINT *)p1; 
      p4 = (POINT *)p2; 
      m = Multiply(tree[0] , *p3 , *p4) ;
      if(m < 0) return 1;
      else if(m == 0 && (Distance(tree[0] , *p3) < Distance(tree[0],*p4)))
          return 1;
      else return -1;
  }
  void Tubao ()
  {
       int i ; 
       result[0].x = tree[0].x;
       result[0].y = tree[0].y;
       result[1].x = tree[1].x;
       result[1].y = tree[1].y;
       result[2].x = tree[2].x;
       result[2].y = tree[2].y;
       top = 2;
       for ( i = 3 ; i <= n ; ++ i )
       {
           while (Multiply(result[top - 1] , result[top] , tree[i]) <= 0 )
                 top -- ;                          //出栈
            result[top + 1].x = tree[i].x ;
            result[top + 1].y = tree[i].y ;
            top ++ ;
       }
  
  }
  int main ()
  {
      int pos ;
      double len , temp , px , py ;
      while ( scanf ( "%d" , &n ) != EOF , n )
      {
            py = -1 ;
            for ( int i = 0 ; i < n ; ++ i )
            {
                scanf ( "%lf%lf" , &tree[i].x , &tree[i].y ) ;
                
            }
            if ( n == 1 )
            {
                 printf ( "0.00\n" ) ;
                 continue ;
            }
            else if ( n == 2 )
            {
                 printf ( "%.2lf\n" , Distance(tree[0] , tree[1]) ) ;
                 continue ;
            }
            for ( int i = 0 ; i < n ; ++ i )
            {
                if(py == -1 || tree[i].y < py)
                    {
                         px = tree[i].x;
                         py = tree[i].y;
                         pos = i;
                    }
                    else if(tree[i].y == py && tree[i].x < px)
                    {
                         px = tree[i].x;
                         py = tree[i].y;
                         pos = i;
                    }
            }
            temp = tree[0].x ;                      // 找出y最小的点 
            tree[0].x = tree[pos].x ;
            tree[pos].x = temp ;
            temp = tree[0].y ;
            tree[0].y = tree[pos].y ;
            tree[pos].y = temp ;
            qsort(&tree[1],n - 1,sizeof(double) * 2,cmp);
            tree[n].x = tree[0].x;
            tree[n].y = tree[0].y;
            Tubao();
            len = 0.0;
            for(int i = 0 ; i < top ; i ++)
                len = len + Distance(result[i] , result[i+1]) ;
           printf("%.2lf\n",len);
    
     }
     return 0 ;
 } 

猜你喜欢

转载自blog.csdn.net/qq_41510496/article/details/80637477