5

POJ 1113 - Wall

 2 years ago
source link: https://exp-blog.com/algorithm/poj/poj1113-wall/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
POJ 1113

给定多边形城堡的n个顶点,绕城堡外面建一个围墙,围住所有点,并且墙与所有点的距离至少为L,求这个墙最小的长度。

推导公式(1)城堡围墙长度最小值 = 城堡顶点坐标构成的散点集的凸包总边长 + 半径为L的圆周长

由于数据规模较大,必须用GrahamScan Algorithm构造凸包(详细的算法可以参考我的 [POJ2187](.//Memory Time
//244K 63MS

#include
#include
#include
using namespace std;

const int inf=10001;
const double pi=3.141592654;

typedef class
{
public:
int x,y;
}point;

/AB距离平方/

int distsquare(point A,point B)
{
return (B.x-A.x)(B.x-A.x)+(B.y-A.y)(B.y-A.y);
}

/AB距离/

double distant(point A,point B)
{
return sqrt((double)((B.x-A.x)(B.x-A.x)+(B.y-A.y)(B.y-A.y)));
}

/叉积计算/

int det(int x1,int y1,int x2,int y2)
{
return x1y2-x2y1;
}

int cross(point A,point B,point C,point D)
{
return det(B.x-A.x,B.y-A.y,D.x-C.x,D.y-C.y);
}

/快排判断规则/

point* s;
int cmp(const void* pa,const void* pb)
{
point* a=(point*)pa;
point* b=(point*)pb;

int temp=cross(*s,*a,*s,*b);
if(temp>0)
    return -1;
else if(temp==0)
    return distsquare(*s,*b)-distsquare(*s,*a);
else
    return 1;

int main(int i,int j)
{
int N,L;
while(cin>>N>>L)
{
/Input/

    point* node=new point[N+1];

    int min_x=inf;
    int fi;
    for(i=1;i<=N;i++)
    {
        cin>>node[i].x>>node[i].y;

        if(min_x > node[i].x)
        {
            min_x = node[i].x;
            fi=i;
        }
        else if(min_x == node[i].x)
            if(node[fi].y > node[i].y)
                fi=i;
    }

    /*Quicksort the Vertex*/

    node[0]=node[N];
    node[N]=node[fi];
    node[fi]=node[0];

    s=&node[N];
    qsort(node+1,N,sizeof(point),cmp);

    /*Structure Con-bag*/

    int* bag=new int[N+2];
    bag[1]=N;
    bag[2]=1;
    int pb=2;
    for(i=2;i<=N;)
        if(cross(node[ bag[pb-1] ],node[ bag[pb] ],node[ bag[pb] ],node[i]) >= 0)
            bag[++pb]=i++;
        else
            pb--;

    /*Compute Min-length*/

    double minlen=0;
    for(i=1;i<pb;i++)
        minlen+=distant(node[ bag[i] ],node[ bag[i+1] ]);

    minlen+=2*pi*L;

    cout<<fixed<<setprecision(0)<<minlen<<endl;

    delete node;
    delete bag;
}
return 0;

------


## 相关资料

- [北大 ACM - POJ 试题分类](https://exp-blog.com/algorithm/poj-shi-ti-fen-lei/)
- [北大 POJ 题库(官网在线)](http://poj.org/)
- [北大 POJ 题库(离线版)](https://github.com/lyy289065406/POJ-Solving-Reports/doc/POJ%E7%A6%BB%E7%BA%BF%E7%89%88%E9%A2%98%E7%9B%AE.chm)
- [POJ封面书《程序设计导引及在线实践》](https://github.com/lyy289065406/POJ-Solving-Reports/doc/程序设计导引及在线实践.pdf)
- [ACM 资料](https://lyy289065406.github.io/articles/tags/ACM/)

Recommend

  • 13
    • blog.woshiluo.com 3 years ago
    • Cache

    POJ 3071 Football

    POJ 3071 Football 2020年9月24日2020年12月16日 by woshiluo 1 题目大意 给定 2n 个整数,从 1 到 2n 编号 给定 pi,...

  • 3

    Armin's BlogPOJ 1611 The Suspects(并查集)November 28, 2015题目链接 题意:非典时期,共有 n 个人(标号 0~n-1),分成 m 组。第一行输入 n(0...

  • 9

    Armin's BlogPOJ 2524 Ubiquitous Religions(并查集)November 29, 2015题目链接 题意:调查学校的学生宗教信仰情况,第一行输入 n(总人数)和 m,...

  • 8
    • zxs.io 3 years ago
    • Cache

    poj 1455 Crazy tea party

    poj 1455 Crazy tea party 2013-10-03 分类:ACM 阅读(4401) 评论(0)   这道题第一眼看去很难,其...

  • 18
    • zxs.io 3 years ago
    • Cache

    poj 1503 高精度加法

    poj 1503 高精度加法 2013-09-07 分类:未分类 阅读(4401) 评论(0) 把输入...

  • 8

    poj 1205 :Water Treatment Plants (DP+高精度) 题意:有n个城市,它们由一个污水处理系统连接着,每个城市可以选择1、将左边城市过来的污水和右边城市过来的污水连同本身的污水排到河里  >V<2、将左边来的污水连同自己的污水排...

  • 14

    poj 2155 Matrix (二维树状数组) 2013-08-31 分类:未分类 阅读(4293) 评论(0) ...

  • 6

    poj 1990 MooFest 树状数组 2013-08-24 分类:未分类 阅读(4320) 评论(0) ...

  • 5
    • atjason.com 2 years ago
    • Cache

    1113 - 体能下降

    1113 - 体能下降 ...

  • 1
    • jandan.net 1 year ago
    • Cache

    无聊图集 1113

    ← 今日好价 1113动物实验显示,朋友越多,肠道有益菌就越丰富 →

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK