HDU 1348 Wall ( 凸包周长 )
**链接:****传送门 **
题意:给出二维坐标轴上 n 个点,这 n 个点构成了一个城堡,国王想建一堵墙,城墙与城堡之间的距离总不小于一个数 L ,求城墙的最小长度,答案四舍五入
思路:城墙与城堡直线长度是相等的,当城堡出现拐角时,城墙必然会出现一段圆弧,这些圆弧最终会构成一个半径为 L 的圆,所以答案就是凸包的周长 + 圆的周长
balabala:
- 采用Jarvis步进法来求凸包,Jarvis步进法复杂度为O(nh),h为凸包顶点个数
- 采用Graham-Scan来求凸包,Graham - Scan 法复杂度为O(nlogn)
如有错误请一定指出!
Graham - Scan法:
/*************************************************************************
> File Name: hdu1348t2.cpp
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年05月07日 星期日 20时49分15秒
************************************************************************/
#include<bits/stdc++.h>
using namespace std;
// Graham - Scan
// O(nlgn)
#define PI 3.1415926535
const int maxn = 1010;
struct point{
double x,y;
};
bool cmp(point a,point b){
return (a.y<b.y || (a.y==b.y && a.x<b.x));
}
bool mult(point sp,point ep,point op){
return (sp.x-op.x)*(ep.y-op.y)>=(sp.y-op.y)*(ep.x-op.x);
}
point res[maxn];
int Graham(point pnt[],int n){
int i , len , k = 0 , top = 1;
sort(pnt,pnt+n,cmp);
if(n == 0) return 0; res[0] = pnt[0];
if(n == 1) return 1; res[1] = pnt[1];
if(n == 2) return 2; res[2] = pnt[2];
for(int i=2;i<n;i++){
while( top && mult( pnt[i] , res[top] , res[top-1] ))
top--;
res[++top] = pnt[i];
}
len = top; res[++top] = pnt[n-2];
for(i=n-3;i>=0;i--){
while( top!=len && mult( pnt[i] , res[top] , res[top-1] ))
top--;
res[++top] = pnt[i];
}
return top;
}
double point_dis(point a,point b){
return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
int main(){
int T , n , L , kase = 0;
point pi[maxn];
scanf("%d",&T);
while(T--){
if(kase > 0) printf("\n");
kase++;
scanf("%d%d",&n,&L);
for(int i=0;i<n;i++) scanf("%lf%lf",&pi[i].x,&pi[i].y);
int t = Graham(pi,n);
double ans = 2*PI*L;
for(int i=0;i<t;i++){
ans += point_dis( res[i] , res[ (i+1)%t ] );
}
printf("%.lf\n",ans);
}
return 0;
}
Jarvis步进法:
/*************************************************************************
> File Name: hdu1348.cpp
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年05月07日 星期日 18时55分57秒
************************************************************************/
#include<bits/stdc++.h>
using namespace std;
#define PI 3.1415926535
const int maxn = 1010;
struct point{
double x,y;
}pi[maxn];
bool cmp(point a,point b){
return ( a.y<b.y || a.y==b.y && a.x<b.x);
}
int n,L,ans[maxn],cnt,sta[maxn],tail;
// 检查是否严格左转,共线不算左转
bool CrossLeft(point p1,point p2,point p3){
return ((p3.x-p1.x)*(p2.y-p1.y) - (p2.x-p1.x)*(p3.y-p1.y)) < 0;
}
void Jarvis(){
tail = cnt = 0;
sort(pi,pi+n,cmp);
sta[tail++] = 0; sta[tail++] = 1;
for(int i=2;i<n;i++){
while(tail>1 && !CrossLeft( pi[ sta[tail-1] ] , pi[ sta[tail-2] ] , pi[i] ))
tail--;
sta[ tail++ ] = i;
}
for(int i=0;i<tail;i++) ans[cnt++] = sta[i];
tail = 0; sta[ tail++ ] = n-1; sta[ tail++ ] = n-2;
for(int i=n-3;i>=0;i--){
while(tail>1 && !CrossLeft( pi[ sta[tail-1] ] , pi[ sta[tail-2] ] , pi[i] ))
tail--;
sta[ tail++ ] = i;
}
for(int i=0;i<tail;i++) ans[cnt++] = sta[i];
}
double Point_dis(point a,point b){
return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
int main(){
int T , kase = 0;
scanf("%d",&T);
while(T--){
if(kase) printf("\n");
kase++;
scanf("%d%d",&n,&L);
for(int i=0;i<n;i++) scanf("%lf%lf",&pi[i].x,&pi[i].y);
Jarvis();
double re = 2*PI*L;
for(int i=0;i<cnt-1;i++){
re += Point_dis( pi[ans[i]] , pi[ans[i+1]] );
}
printf("%.0lf\n",re);
}
return 0;
}
HDU 1348 Wall ( 凸包周长 )的更多相关文章
- hdu 1348 Wall (凸包模板)
/* 题意: 求得n个点的凸包.然后求与凸包相距l的外圈的周长. 答案为n点的凸包周长加上半径为L的圆的周长 */ # include <stdio.h> # include <ma ...
- hdu 1348 Wall (凸包)
Wall Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu 1348:Wall(计算几何,求凸包周长)
Wall Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu 1348 (凸包求周长)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 Wall Time Limit: 2000/1000 MS (Java/Others) Mem ...
- hdu 1348 Wall(凸包模板题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 Wall Time Limit: 2000/1000 MS (Java/Others) M ...
- HDU 1348 Wall 【凸包】
<题目链接> 题目大意: 给出二维坐标轴上 n 个点,这 n 个点构成了一个城堡,国王想建一堵墙,城墙与城堡之间的距离总不小于一个数 L ,求城墙的最小长度,答案四舍五入. 解题分析: 求 ...
- HDU 1348 Wall
题解:计算凸包周长 #include <iostream> #include <cmath> #include <algorithm> const int size ...
- POJ 1113 || HDU 1348: wall(凸包问题)
传送门: POJ:点击打开链接 HDU:点击打开链接 以下是POJ上的题: Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...
- hdu 1348【凸包模板】
#include<iostream> #include<iostream> #include<algorithm> #include<cmath> us ...
随机推荐
- HTTPie:一个不错的 HTTP 命令行客户端
转自:http://top.jobbole.com/9682/ HTTPie:一个不错的 HTTP 命令行客户端 HTTPie (读aych-tee-tee-pie)是一个 HTTP 的命令行客户端. ...
- 重置浏览器默认样式 normalize.css
1 /*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */ /* Document ========= ...
- 利用Tensorflow实现手写字符识别
模式识别领域应用机器学习的场景非常多,手写识别就是其中一种,最简单的数字识别是一个多类分类问题,我们借这个多类分类问题来介绍一下google最新开源的tensorflow框架,后面深度学习的内容都会基 ...
- 电脑-制作WIN7启动U盘
1.需要准备的工具:win7系统盘(安装盘,不是ghost),软碟通工具,大于4G的U盘
- Android Handler 具体解释
Android开发中常常使用Handler来实现"跨越线程(Activity)更新UI".本文将从源代码角度回答:为什么使用Handler可以跨线程更新UI?为什么跨线程更新UI一 ...
- UVA 1149 Bin Packing 二分+贪心
A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the samele ...
- h5-爆料view
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdsAAABeCAIAAADkEim8AAAWAElEQVR4nO2d+1Nb55nHPbMz+1v+g/ ...
- SQL Server loop - how do I loop through a set of records
SQL Server loop - how do I loop through a set of records By using T-SQL and cursors like this : DECL ...
- hdoj--2579--Dating with girls(2)(搜索+三维标记)
Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- ROS-动态参数
前言:在节点外部改变参数的方式有:参数服务器.服务.主题以及动态参数. 1.新建cfg文件 在chapter2_tutorials包下新建cfg文件夹,在cfg文件夹下新建chapter2.cfg文件 ...