凸包模板

POJ - 1113

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
结果四舍五入就可以了

题意:给你一些点 求周长。  

分析:周长是一个圆加凸包的周长。


#include<iostream>  
#include<string>  
#include<cstring>  
#include<algorithm>  
#include<cstdio>  
#include<cmath>  
#include<cctype>  
#include<iomanip>  
#include<vector>  
#include<map>  
#include<set>  
using namespace std;  
  
  
#define LL long long  
#define INF 1E4 * 1E9  
#define pi acos(-1)  
#define endl '\n'  
#define me(x) memset(x,0,sizeof(x));  
const int maxn=1e3+5;  
const int maxx=1e6+5;  
  
  
struct node  
{  
    double x,y;  
}p[maxn],P[maxn];// p[] 存点 P[] 存凸包  
  
int n,tot;//tot 凸包的点  
double ans,l;  
double X(node A,node B,node C)//差积 是否<0  
{  
    return (B.x-A.x)*(C.y-A.y)-(C.x-A.x)*(B.y-A.y);  
}  
  
double len(node A,node B)//距离  
{  
    return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));  
}  
  
bool cmp(node A,node B)  
{  
    double pp=X(p[0],A,B);  
    if(pp>0)  return 1;  
    if(pp<0)  return 0;  
    return len(p[0],A)<len(p[0],B); //如果等于0 判断距离  返回小的  
}  
  
int main()  
{  
    cin>>n>>l;  
    ans=2*pi*l;  
    for(int i=0;i<n;i++)  
        cin>>p[i].x>>p[i].y;//点的坐标  
    if(n==1)  
        printf("%.0f\n",ans);//等于1的话就是一个点  
    else if(n==2)  
        printf("%.0f\n",ans+len(p[0],p[1])); // 2个点 一条直线  
    else//求凸包的操作  
    {  
        for(int i=0;i<n;i++)  
        {  
            if(p[i].y<p[0].y)  swap(p[i],p[0]);//找出 y轴最小的点  
            else if(p[i].y==p[0].y&&p[i].x<p[0].x)  swap(p[i],p[0]);//x坐标小的  
        }  
    }  
    sort(p+1,p+n,cmp);  
    P[0]=p[0];  
    P[1]=p[1];  
    tot=1;  
    for(int i=2;i<n;i++)  
    {  
        while(tot>0&&X(P[tot-1],P[tot],p[i])<=0) tot--;// P[tot-1]上一个点  
        tot++;  
        P[tot]=p[i];//存入凸包  
    }  
    for(int i=0;i<tot;i++)  ans+=len(P[i],P[i+1]);//ans加周长  
    ans+=len(P[0],P[tot]);  
    printf("%.0f\n",ans);  
}  



POJ - 1228

Being the only living descendant of his grandfather, Kamran the Believer inherited all of the grandpa's belongings. The most valuable one was a piece of convex polygon shaped farm in the grandpa's birth village. The farm was originally separated from the neighboring farms by a thick rope hooked to some spikes (big nails) placed on the boundary of the polygon. But, when Kamran went to visit his farm, he noticed that the rope and some spikes are missing. Your task is to write a program to help Kamran decide whether the boundary of his farm can be exactly determined only by the remaining spikes.
Input
The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains an integer n (1 <= n <= 1000) which is the number of remaining spikes. Next, there are n lines, one line per spike, each containing a pair of integers which are x and y coordinates of the spike.
Output
There should be one output line per test case containing YES or NO depending on whether the boundary of the farm can be uniquely determined from the input.
Sample Input
1
6 
0 0
1 2
3 4
2 0
2 4 
5 0
Sample Output
NO

题意:问这个凸包是不是稳定的。  就是问凸包的顶点相连 这个连线是否有一个点支撑  如果有  这个凸边就是稳定的  然后问这个凸包是不是稳定的,。

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
using namespace std;


#define LL long long
#define INF 1E4 * 1E9
#define pi acos(-1)
#define endl '\n'
#define me(x) memset(x,0,sizeof(x));
const int maxn=1e3+5;
const int maxx=1e6+5;

struct node
{
    double x,y;
}p[maxn],P[maxn];// p[] 存点 P[] 存凸包

int n,tot;//tot 凸包的点
double ans,l;
double X(node A,node B,node C)//差积 是否<0
{
    return (B.x-A.x)*(C.y-A.y)-(C.x-A.x)*(B.y-A.y);
}

double len(node A,node B)//距离
{
    return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}

int cmp(node A,node B)
{
    double pp=X(p[0],A,B);
    if(pp>0)  return 1;
    if(pp<0)  return 0;
    return len(p[0],A)<len(p[0],B); //如果等于0 判断距离  返回小的
}

int judge()
{
    for(int i=1;i<tot-1;i++)
    {
        if((X(P[i-1],P[i+1],P[i]))!=0&&X(P[i],P[i+2],P[i+1])!=0)          //判断每条边是否有至少三个点;
            return 0;
    }
    return 1;
}
int main()
{
    int t;
    cin>>t;
    for(int cas=1;cas<=t;cas++)
    {
        //if(cas!=1) cout<<endl;
        cin>>n;
        //ans=2*pi*l;

        for(int i=0;i<n;i++)
            cin>>p[i].x>>p[i].y;//点的坐标
        if(n<6)
        {
            puts("NO");
            continue;
        }
            //printf("%.0f\n",ans);//等于1的话就是一个点
        //else if(n==2)
           // printf("%.0f\n",ans+len(p[0],p[1])); // 2个点 一条直线
        else//求凸包的操作
        {
            for(int i=0;i<n;i++)
            {
                if(p[i].y<p[0].y)  swap(p[i],p[0]);//找出 y轴最小的点
                else if(p[i].y==p[0].y&&p[i].x<p[0].x)  swap(p[i],p[0]);//x坐标小的
            }
        }
        sort(p+1,p+n,cmp);
        P[0]=p[0];
        P[1]=p[1];
        tot=1;
        for(int i=2;i<n;i++)
        {
            while(tot>0&&X(P[tot-1],P[tot],p[i])<0) tot--;// P[tot-1]上一个点
            tot++;
            P[tot]=p[i];//存入凸包
        }
        //cout << "___________"<<endl;
        //for(int i=0;i<=tot;i++)//ans+=len(P[i],P[i+1]);//ans加周长
           // cout << P[i].x << ' ' << P[i].y <<endl;
        //ans+=len(P[0],P[tot]);
        //printf("%.0f\n",ans);
         if(judge())  puts("YES");
         else puts("NO");
    }

}
下面附上水平排序的方法求凸包;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define db double

const int MAX = 1e3+7;
//const int maxn = 3e6+5;
const int mod = 1000000007;
const db eps = 1e-7;
int ca = 1;

struct PO
{
    int x, y;
    PO (){};
    PO (int x, int y):x(x), y(y){};
    friend PO operator - (const PO&a, const PO&b)
    {
        return PO(a.x-b.x, a.y-b.y);
    }
};
PO pos[MAX];//存点
PO ch[MAX];//栈
bool cmp(const PO&a, const PO&b)
{
    if(a.x==b.x)return a.y<b.y;
    else return a.x<b.x;
}
int cross(const PO&a, const PO&b)
{
    return a.x*b.y-a.y*b.x;
}

int n;

void solve()
{
    scanf("%d", &n);
    for(int i=0; i<n; i++)
        scanf("%d%d", &pos[i].x, &pos[i].y);
    sort(pos, pos+n, cmp);
    //第一条链
    int m = 0;
    for(int i=0; i<n; i++)
    {
        while(m>1 && cross(pos[i]-ch[m-2], ch[m-1]-ch[m-2])>=0) m--;
        ch[m++] = pos[i];
    }
    //第二条链
    int temp = m;
    for(int i=n-2; i>=0; i--)
    {
        while(m>temp && cross(pos[i]-ch[m-2], ch[m-1]-ch[m-2])>=0) m--;
        ch[m++] = pos[i];
    }
    if(n>1) m--;
    cout << "______________"<<endl;
    for(int i=0; i<m; i++)
        printf("%d %d\n", ch[i].x, ch[i].y);
}

int main()
{
    //Frei
    //Freo
    int t = 1;
    //cin >> t;
    while (t--) solve();

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值