HDU 1348 Wall 【凸包】
<题目链接>
题目大意:
给出二维坐标轴上 n 个点,这 n 个点构成了一个城堡,国王想建一堵墙,城墙与城堡之间的距离总不小于一个数 L ,求城墙的最小长度,答案四舍五入.
解题分析:
求出这些点所围成的凸包,然后所围城墙的长度就为 该凸包周长 + 以该距离为半径的圆的周长。具体证明如下:
下面的模板还没有整理好
Graham 凸包算法
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define maxn 1100
const double pi = acos(-1.0); struct Point {
int x, y;
}s[maxn]; int st[maxn], top; int cross(Point p, Point p1, Point p2)
{
return (p1.x - p.x)*(p2.y - p.y) - (p2.x - p.x)*(p1.y - p.y);
} double dist(Point p1, Point p2)
{
double tmp = (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y);
return sqrt(tmp); } bool cmp(Point p1, Point p2)
{
int tmp = cross(s[], p1, p2);
if (tmp>) return true;
else if (tmp == && dist(s[], p1)<dist(s[], p2)) return true;
else return false;
} void Graham(int n)
{
if (n == ) { top = ; st[] = ; }
if (n == ) { top = ; st[] = ; st[] = ; }
if (n>)
{
int i;
st[] = , st[] = ;
top = ;
for (i = ; i<n; i++)
{
while (top> && cross(s[st[top - ]], s[st[top]], s[i])<) //不习惯s[st[top-1]]这种代码风格的可以改一下
top--;
st[++top] = i;
}
}
} int main()
{
int n, l;
int ncase;
cin >> ncase;
while (ncase--)
{
scanf("%d %d", &n, &l);
Point p0;
scanf("%d%d", &s[].x, &s[].y);
p0.x = s[].x, p0.y = s[].y;
int k = ;
for (int i = ; i < n; i++)
{
scanf("%d%d", &s[i].x, &s[i].y);
if ((p0.y > s[i].y) || (p0.y == s[i].y&&p0.x > s[i].x))
{
p0.x = s[i].x;
p0.y = s[i].y;
k = i;
}
}
s[k] = s[];
s[] = p0; sort(s + , s + n, cmp);
Graham(n);
double ans = ;
for (int i = ; i < top; i++)
{
ans += dist(s[st[i]], s[st[i + ]]);
}
ans += dist(s[st[]], s[st[top]]);
ans += * pi*l;
printf("%d\n", (int)(ans + 0.5));
if (ncase)printf("\n");
}
return ;
}
Andrew算法
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const double Pi=acos(-1.0);
struct Point{
double x,y;
Point(double x=,double y=):x(x),y(y){}
};
typedef Point Vector;
bool operator < (Point a,Point b){return a.x<b.x||(a.x==b.x&&a.y<b.y);} Vector operator - (Point a,Point b){return Vector(a.x-b.x,a.y-b.y);} double Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;} double Length(Vector a){return sqrt(Dot(a,a));} double Angle(Vector a,Vector b){return acos(Dot(a,b)/Length(a)/Length(b));} Vector Rotate(Vector a,double rad){return Vector(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));} double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;} Point operator + (Point a,Vector b){return Point(a.x+b.x,a.y+b.y);} Point getdot(Point a,Vector b,double ang){return a+Rotate(b,ang);} double getrad(double ang){return Pi*(ang/);} Point ans[],at[];
int nu; double polygonArea(){
int k=;
for(int i=;i<nu;i++){
while(k>&&Cross(ans[k-]-ans[k-],at[i]-ans[k-])<=)k--;
ans[k++]=at[i];
}
int p=k;
for(int i=nu-;i>=;i--){
while(k>p&&Cross(ans[k-]-ans[k-],at[i]-ans[k-])<=)k--;
ans[k++]=at[i];
}
double x=;
k--;
if(k<)return ; //求该凸多边形面积
for(int i=;i<k-;i++)x+=Cross(ans[i]-ans[],ans[i+]-ans[]);
return x/;
} int main(){
int T,n;
double x,y,w,h,ang;
scanf("%d",&T);
while(T--){
double area1=,area2=;
nu=;
scanf("%d",&n);
while(n--){
scanf("%lf%lf%lf%lf%lf",&x,&y,&w,&h,&ang);
area2+=w*h; //area储存所有矩形的总面积
Point a;
ang=-getrad(ang);//因为是顺时针旋转的,所以要是负的。。。。。
at[nu++]=getdot(Point(x,y),Vector(w/,h/),ang);
at[nu++]=getdot(Point(x,y),Vector(-w/,h/),ang);
at[nu++]=getdot(Point(x,y),Vector(w/,-h/),ang);
at[nu++]=getdot(Point(x,y),Vector(-w/,-h/),ang);
}
sort(at,at+nu);
area1=polygonArea();
// printf("%lf %lf\n",area1,area2);
printf("%.1lf %%\n",*area2/area1);
}
return ;
}
2018-08-04
HDU 1348 Wall 【凸包】的更多相关文章
- hdu 1348 Wall (凸包)
Wall Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu 1348 Wall (凸包模板)
/* 题意: 求得n个点的凸包.然后求与凸包相距l的外圈的周长. 答案为n点的凸包周长加上半径为L的圆的周长 */ # include <stdio.h> # include <ma ...
- hdu 1348 Wall(凸包模板题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 Wall Time Limit: 2000/1000 MS (Java/Others) M ...
- hdu 1348 (凸包求周长)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 Wall Time Limit: 2000/1000 MS (Java/Others) Mem ...
- POJ 1113 || HDU 1348: wall(凸包问题)
传送门: POJ:点击打开链接 HDU:点击打开链接 以下是POJ上的题: Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...
- hdu 1348:Wall(计算几何,求凸包周长)
Wall Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- HDU 1348 Wall ( 凸包周长 )
链接:传送门 题意:给出二维坐标轴上 n 个点,这 n 个点构成了一个城堡,国王想建一堵墙,城墙与城堡之间的距离总不小于一个数 L ,求城墙的最小长度,答案四舍五入 思路:城墙与城堡直线长度是相等的, ...
- HDU 1348 Wall
题解:计算凸包周长 #include <iostream> #include <cmath> #include <algorithm> const int size ...
- hdu 1348【凸包模板】
#include<iostream> #include<iostream> #include<algorithm> #include<cmath> us ...
随机推荐
- FPN-Feature Pyramid Networks for Object Detection
FPN-Feature Pyramid Networks for Object Detection 标签(空格分隔): 深度学习 目标检测 这次学习的论文是FPN,是关于解决多尺度问题的一篇论文.记录 ...
- spring-framework-x.x.x.RELEASE-dist下载教程
1.打开Spring官网:https://spring.io,点击PROJECTS 2.点击SPRING FRAMEWORK 3.点击GitHub图标 4.找到Access to Binaries,点 ...
- Kaggle 泰坦尼克
入门kaggle,开始机器学习应用之旅. 参看一些入门的博客,感觉pandas,sklearn需要熟练掌握,同时也学到了一些很有用的tricks,包括数据分析和机器学习的知识点.下面记录一些有趣的数据 ...
- 六、regularized logisitic regssion练习(转载)
转载链接:http://www.cnblogs.com/tornadomeet/archive/2013/03/17/2964858.html 在上一讲Deep learning:五(regulari ...
- nodejs async waterfull 小白向
async.waterfall([function(callback){var a=3+5;callback(null,a);},function(n,callback) { callback(nul ...
- Node.jsでMySQLを使うメモ
インストール npm install mysql コネクション var mysql = require('mysql'); var connection = mysql.createConnectio ...
- java并发编程系列四、AQS-AbstractQueuedSynchronizer
什么是AbstractQueuedSynchronizer?为什么我们要分析它? AQS:抽象队列同步器,原理是:当多个线程去获取锁的时候,如果获取锁失败了,当前线程就会被打包成一个node节点放入 ...
- 默认以管理员身份运行VS2013/15/17
方法如下: 1.右击VS的快捷方式,选择[属性],打开属性对话框,再点击[高级]按钮,如下图所示: 2.再勾选[用管理员身份运行],点击[确定]即可: 然后就可以双击VS快捷方式,直接以管理员身份运行 ...
- 一步步实现windows版ijkplayer系列文章之六——SDL2源码分析之OpenGL ES在windows上的渲染过程
一步步实现windows版ijkplayer系列文章之一--Windows10平台编译ffmpeg 4.0.2,生成ffplay 一步步实现windows版ijkplayer系列文章之二--Ijkpl ...
- 关于ftp上传changeWorkingDirectory()方法的路径切换问题
在上传时 FTPClient提供了upload方法,对于upload(file,path)的第二个参数path ,上传到哪里的这个路径, ftp是利用changeWorkingDirectory()方 ...