HDU - 1392 Surround the Trees (凸包)

Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392

题意:

  在给定点中找到凸包,计算这个凸包的周长。

思路:

  这道题找出凸包上的点后,s数组中就是按顺序的点,累加一下距离就是周长了。

#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>

using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue

typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl ‘\n‘

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const int mod = 10007;
const double esp = 1e-8;
const double PI=acos(-1.0);

template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<‘0‘||ch>‘9‘) f|=(ch==‘-‘),ch=getchar();
    while (ch>=‘0‘&&ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar();
    return x=f?-x:x;
}

/*-----------------------showtime----------------------*/
            const int maxn = 120;
            struct node
            {
                int x,y;
            }p[maxn],s[maxn];
            inline bool cmp(node a,node b){
                double A = atan2((a.y - p[1].y) , (a.x - p[1].x));
                double B = atan2((b.y - p[1].y) , (b.x - p[1].x));
                if(A != B) return A < B;
                else return a.x < b.x;
            }

            ll Cross (node a,node b,node c){
                return 1ll*(b.x - a.x)*(c.y - a.y) - 1ll*(b.y - a.y)*(c.x - a.x);
            }
            int top,n;
            void Get(){
                p[0] = (node){inf,inf}; int k;
                for(int i=1; i<=n; i++)
                    if(p[0].y > p[i].y || (p[0].y == p[i].y && p[i].x < p[0].x))
                    {
                        p[0] = p[i];    k = i;
                    }
                swap(p[k], p[1]);
                sort(&p[2] , &p[n+1], cmp);
                s[0] = p[1] , s[1] = p[2], top = 1;
                for(int i=3; i<=n; ){
                    if(top && Cross(s[top-1] ,p[i], s[top]) >= 0)
                        top--;
                    else s[++top] = p[i++];
                }
            }
            double dis(node a,node b){
                return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
            }
int main(){     

            while(~scanf("%d", &n) && n){
                for(int i=1; i<=n; i++){
                    scanf("%d%d", &p[i].x, &p[i].y);
                }
                Get();

                double ans = 0;
                if(top == 0){
                    ans = 0;
                }
                else if(top == 1){
                    ans = dis(s[0],s[1]);
                }
                else {
                    s[++top] = s[0];
                    for(int i=0; i<top; i++){
                        ans += dis(s[i], s[i+1]);
                    }
                }

                printf("%.2f\n", ans);
            }
            return 0;
}       

HDU - 1392

原文地址:https://www.cnblogs.com/ckxkexing/p/9665444.html

时间: 2024-08-29 07:18:27

HDU - 1392 Surround the Trees (凸包)的相关文章

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

HDU 1392 Surround the Trees (凸包周长)

题目链接:HDU 1392 Problem 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

hdu 1392 Surround the Trees(凸包果题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7473    Accepted Submission(s): 2860 Problem Description There are a lot o

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

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

HDU 1392.Surround the Trees【凸包(求凸包周长)】【5月10】

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

HDU 1392 Surround the Trees(几何 凸包模板)

http://acm.hdu.edu.cn/showproblem.php?pid=1392 题目大意: 二维平面给定n个点,用一条最短的绳子将所有的点都围在里面,求绳子的长度. 解题思路: 凸包的模板.凸包有很多的算法.这里用Adrew. 注意这几组测试数据 1 1 1 3 0 0 1 0 2 0 输出数据 0.00 2.00 1 #include<cmath> 2 #include<cstdio> 3 #include<algorithm> 4 using name

HDU 1392 Surround the Trees (Graham求凸包周长)

题目链接 题意 : 让你找出最小的凸包周长 . 思路 : 用Graham求出凸包,然后对每条边求长即可. Graham详解 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <math.h> 5 #include <algorithm> 6 7 using namespace std ; 8 9 struct point 10 { 11 int

杭电 1392 Surround the Trees

经典凸包问题!!!! AC代码如下: #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<cstdio> using namespace std; struct H { double x,y; }trees[105]; bool cmp(H a,H b) { return a.x<b.x||(a.x==b.x&&a