HDU1392 Surround the Trees

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

题目链接:HDU1392

正解:凸包

解题报告:

  凸包模板题。

//It is made by ljh2000
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <complex>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <queue>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#define lc root<<1
#define rc root<<1|1
#define pr pair<int,int>
#define MP make_pair
#define fr first
#define sc second
#define rep(i,j,k) for(int i=j;i<=k;++i)
#define per(i,j,k) for(int i=j;i>=k;--i)
#define reg(i,x) for(int i=first[x];i;i=next[i])
using namespace std;
typedef long long LL;
typedef long double LB;
typedef complex<double> C;
const double pi = acos(-1);
const double eps = 1e-9;
const int MAXN = 1011;
int n,top,stack[MAXN];
struct node{ double x,y; }a[MAXN];
inline node operator - (const node &q,const node &qq) { return (node){q.x-qq.x,q.y-qq.y}; }
inline double cross(node q,node qq){ return q.x*qq.y-q.y*qq.x; }
inline bool cmp(node q,node qq){ if(q.x==qq.x) return q.y<qq.y; return q.x<qq.x; }
inline double sqr(LL x){ return x*x; }
inline double dis(node q,node qq){ return sqrt(sqr(q.x-qq.x)+sqr(q.y-qq.y)); }
inline int getint(){
    int w=0,q=0; char c=getchar(); while((c<‘0‘||c>‘9‘) && c!=‘-‘) c=getchar();
    if(c==‘-‘) q=1,c=getchar(); while (c>=‘0‘&&c<=‘9‘) w=w*10+c-‘0‘,c=getchar(); return q?-w:w;
}

inline void work(){
	while(1) {
		n=getint(); if(n==0) break;
		for(int i=1;i<=n;i++) a[i].x=getint(),a[i].y=getint();
		if(n==1) printf("%.2lf\n",0.00);
        else if(n==2) printf("%.2lf\n",dis(a[1],a[2]));
		else {
			sort(a+1,a+n+1,cmp); stack[top=1]=1;
			for(int i=2;i<=n;i++) {
				while(top>1 && cross( a[stack[top]]-a[stack[top-1]] , a[i]-a[stack[top-1]]) <= 0 ) top--;
				stack[++top]=i;
			}
			int k=top;
			for(int i=n;i>=1;i--) {
				while(top>k && cross( a[stack[top]]-a[stack[top-1]] , a[i]-a[stack[top-1]]) <= 0 ) top--;
				stack[++top]=i;
			}
			if(n!=1) top--; double ans=0;
			for(int i=1;i<=top/*!!!*/;i++) ans+=dis(a[stack[i]],a[stack[i+1]]);
			printf("%.2lf\n",ans);
		}
	}
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("1392.in","r",stdin);
	freopen("1392.out","w",stdout);
#endif
    work();
    return 0;
}
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。

  

时间: 2024-10-31 23:26:23

HDU1392 Surround the Trees的相关文章

ACM学习历程—HDU1392 Surround the Trees(计算几何)

Description There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you help him?        The

凸包模版 HDU1392 Surround the Trees

题目链接 题目大意 给出n颗树的坐标,要用一根绳子将所有的树围起来,求绳子的最短长度. 解题思路 求这n个坐标能形成的最大凸包.需要特判n为1时绳子长度为0以及n为2时绳子长度为2树距离.剩下的套凸包模版即可. AC代码 #include <iostream> #include <cmath> #include <cstdio> #include <cstring> #define eps 0.00000001 using namespace std; /*

HDU1392:Surround the Trees(凸包问题)

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7164    Accepted Submission(s): 2738 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to

Surround the Trees[HDU1392]

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11811    Accepted Submission(s): 4577 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to

HDUJ 1392 Surround the Trees 凸包

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7203    Accepted Submission(s): 2752 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to

hdu 1392 Surround the Trees (凸包)

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7043    Accepted Submission(s): 2688 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to

zoj 1453 Surround the Trees(凸包求周长)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=453 Time Limit: 2 Seconds      Memory Limit: 65536 KB There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal r

Surround the Trees(凸包)

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 291    Accepted Submission(s): 140   Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to

HDU 1392 Surround the Trees

PS: 在求解两个点的时候就是两个点的距离,在这WA了一次. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <cmath> using namespace std; const int maxn = 110; struct point { int x, y; point(d