HDOJ 1348 Wall 凸包

Wall

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4001    Accepted Submission(s): 1131

Problem 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.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Sample Input

1

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

Sample Output

1628

Source

Northeastern Europe 2001

/* ***********************************************
Author        :CKboss
Created Time  :2015年04月07日 星期二 21时12分04秒
File Name     :HDOJ1348.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

const double eps=1e-7;

int dcmp(double x) { if(fabs(x)<eps) return 0; return (x<0)?-1:1; }

int n;
double r;

struct Point
{
	double x,y;
	Point(double _x=0,double _y=0):x(_x),y(_y){};
};

Point operator+(Point A,Point B) { return Point(A.x+B.x,A.y+B.y); }
Point operator-(Point A,Point B) { return Point(A.x-B.x,A.y-B.y); }
Point operator*(Point A,double p) { return Point(A.x*p,A.y*p); }
Point operator/(Point A,double p) { return Point(A.x/p,A.y/p); }

bool operator<(const Point& a,const Point& b) { return a.x<b.x||(a.x==b.x&&a.y<b.y); }
bool operator==(const Point& a,const Point& b) { return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0; }

double Dot(Point A,Point B){return A.x*B.x+A.y*B.y;}
double Length(Point A) {return sqrt(Dot(A,A));}
double Cross(Point A,Point B) { return A.x*B.y-A.y*B.x; }

vector<Point> CovexHull(vector<Point>& p)
{
	sort(p.begin(),p.end());
	p.erase(unique(p.begin(),p.end()),p.end());
	int n=p.size();
	int m=0;
	vector<Point> ch(n+1);
	for(int i=0;i<n;i++)
	{
		while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
		ch[m++]=p[i];
	}
	int k=m;
	for(int i=n-2;i>=0;i--)
	{
		while(m>k&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
		ch[m++]=p[i];
	}
	if(n>1) m--;
	ch.resize(m);
	return ch;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

	int T_T;
	scanf("%d",&T_T);
	while(T_T--)
	{
		scanf("%d%lf",&n,&r);
		vector<Point> vp;
		for(int i=0;i<n;i++)
		{
			double X,Y;
			scanf("%lf%lf",&X,&Y);
			vp.push_back(Point(X,Y));
		}
		vector<Point> CH=CovexHull(vp);
		CH.push_back(CH[0]);
		double ans = 2.*acos(-1.0)*r;
		int sz=CH.size();
		for(int i=1;i<sz;i++)
		{
			double duan = Length(CH[i]-CH[i-1]);
			ans+=duan;
		}
		printf("%.0lf\n",ans);
		if(T_T) putchar(10);
	}

    return 0;
}
时间: 2024-10-11 12:15:02

HDOJ 1348 Wall 凸包的相关文章

hdu 1348 Wall (凸包)

Wall Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3139    Accepted Submission(s): 888 Problem Description Once upon a time there was a greedy King who ordered his chief Architect to build a w

hdu 1348 Wall (凸包模板)

/* 题意: 求得n个点的凸包,然后求与凸包相距l的外圈的周长. 答案为n点的凸包周长加上半径为L的圆的周长 */ # include <stdio.h> # include <math.h> # include <string.h> # include <algorithm> using namespace std; # define PI acos(-1.0) struct node { int x; int y; }; node a[1010],res

hdu 1348 Wall(凸包模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 Wall Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3386    Accepted Submission(s): 968 Problem Description Once upon a time there was a gre

POJ 1113 || HDU 1348: wall(凸包问题)

传送门: POJ:点击打开链接 HDU:点击打开链接 下面是POJ上的题: Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29121   Accepted: 9746 Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's cast

hdu 1348 (凸包求周长)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 Wall Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3229    Accepted Submission(s): 919 Problem Description Once upon a time there was a greedy

【HDOJ】1348 Wall

计算几何-凸包模板题目,Graham算法解. 1 /* 1348 */ 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cstdlib> 6 #include <cmath> 7 #include <algorithm> 8 using namespace std; 9 10 #define MAXN 1005 11 12 typed

HDU 1348 Wall 【凸包】

<题目链接> 题目大意: 给出二维坐标轴上 n 个点,这 n 个点构成了一个城堡,国王想建一堵墙,城墙与城堡之间的距离总不小于一个数 L ,求城墙的最小长度,答案四舍五入. 解题分析: 求出这些点所围成的凸包,然后所围城墙的长度就为 该凸包周长 + 以该距离为半径的圆的周长.具体证明如下: 下面的模板还没有整理好 Graham 凸包算法 #include<iostream> #include<cstdio> #include<cmath> #include&

poj1113 Wall 凸包

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 perfec

poj 1113 Wall (凸包模板题)

Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32808   Accepted: 11137 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 w