UVa 10652 (简单凸包) Board Wrapping
题意:
有n块互不重叠的矩形木板,用尽量小的凸多边形将它们包起来,并输出并输出木板总面积占凸多边形面积的百分比。
分析:
几乎是凸包和多边形面积的裸题。
注意:最后输出的百分号前面有个空格,第一次交PE了。
用printf打印%,可以连续打印两个%%,printf("%%\n"); 这个冷知识记得以前学过,不过不用也就忘了。
学习一下vector容器中去重的小技巧。
sort(p.begin(), p.end());
p.erase(unique(p.begin(), p.end()), p.end());
ConvexHull函数最后用到了resize(n),顾名思义就是改变容器的大小为n。超过该范围的元素无效,下次push_back进来的元素将在第n个后面。
//#define LOCAL
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
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;
Point operator + (Vector A, Vector B) { return Vector(A.x+B.x, A.y+B.y); }
Point operator - (Vector A, Vector B) { return Vector(A.x-B.x, A.y-B.y); }
double Cross(const Vector& A, const Vector& B) { return A.x*B.y - A.y*B.x; }
Vector Rotate(Vector A, double p)
{
return Vector(A.x*cos(p)-A.y*sin(p), A.x*sin(p)+A.y*cos(p));
}
bool operator < (const Point& A, const Point& B)
{
return A.x < B.x || (A.x == B.x && A.y < B.y);
}
bool operator == (const Vector& A, const Vector& B)
{
return A.x == B.x && A.y == B.y;
} double torad(double x) { return x / 180.0 * PI; } vector<Point> ConvexHull(vector<Point> p)
{
//Ô¤´¦Àí£¬È¥ÖØ
sort(p.begin(), p.end());
p.erase(unique(p.begin(), p.end()), p.end()); int n = p.size();
int m = ;
vector<Point> ch(n+);
for(int i = ; i < n; ++i)
{
while(m > && Cross(ch[m-]-ch[m-], p[i]-ch[m-]) <= ) m--;
ch[m++] = p[i];
}
int k = m;
for(int i = n-; i >= ; --i)
{
while(m > k && Cross(ch[m-]-ch[m-], p[i]-ch[m-]) <= ) m--;
ch[m++] = p[i];
}
if(n > ) m--;
ch.resize(m);
return ch;
} double PolygonArea(vector<Point> p)
{
int n = p.size();
double ans = 0.0;
for(int i = ; i < n-; ++i)
ans += Cross(p[i]-p[], p[i+]-p[]);
return ans/;
} int main(void)
{
#ifdef LOCAL
freopen("10652in.txt", "r", stdin);
#endif int T;
scanf("%d", &T);
while(T--)
{
int n;
vector<Point> p;
double area1 = 0.0;
scanf("%d", &n);
for(int i = ; i < n; ++i)
{
double x, y, w, h, a;
scanf("%lf%lf%lf%lf%lf", &x, &y, &w, &h, &a);
Point o(x, y);
double ang = -torad(a);
p.push_back(o + Rotate(Vector(w/, h/), ang));
p.push_back(o + Rotate(Vector(-w/, h/), ang));
p.push_back(o + Rotate(Vector(w/, -h/), ang));
p.push_back(o + Rotate(Vector(-w/, -h/), ang));
area1 += w*h;
}
double area2 = PolygonArea(ConvexHull(p));
printf("%.1lf %%\n", area1 / area2 * 100.0);
}
}
代码君
UVa 10652 (简单凸包) Board Wrapping的更多相关文章
- uva 10652 Board Wrapping (计算几何-凸包)
Problem B Board Wrapping Input: standard input Output: standard output Time Limit: 2 seconds The sma ...
- UVA 10652 Board Wrapping 计算几何
多边形凸包.. .. Board Wrapping Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu ...
- 简单几何(向量旋转+凸包+多边形面积) UVA 10652 Board Wrapping
题目传送门 题意:告诉若干个矩形的信息,问他们在凸多边形中所占的面积比例 分析:训练指南P272,矩形面积长*宽,只要计算出所有的点,用凸包后再求多边形面积.已知矩形的中心,向量在原点参考点再旋转,角 ...
- Uva 10652 Board Wrapping(计算几何之凸包+点旋转)
题目大意:给出平面上许多矩形的中心点和倾斜角度,计算这些矩形面积占这个矩形点形成的最大凸包的面积比. 算法:GRAHAM,ANDREW. 题目非常的简单,就是裸的凸包 + 点旋转.这题自己不会的地方就 ...
- UVA 10652 Board Wrapping(凸包)
The small sawmill in Mission, British Columbia, hasdeveloped a brand new way of packaging boards for ...
- UVA 10652 Board Wrapping(凸包)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32286 [思路] 凸包 根据角度与中心点求出长方形所有点来,然后就 ...
- UVA 10652 Board Wrapping(二维凸包)
传送门 刘汝佳<算法竞赛入门经典>P272例题6包装木板 题意:有n块矩形木板,你的任务是用一个面积尽量小的凸多边形把它们抱起来,并计算出木板占整个包装面积的百分比. 输入:t组数据,每组 ...
- uva 10652 Board Wrapping
主要是凸包的应用: #include <cstdio> #include <cmath> #include <cstring> #include <algor ...
- ●UVA 10652 Board Wrapping
题链: https://vjudge.net/problem/UVA-10652 题解: 计算几何,Andrew求凸包, 裸题...(数组开小了,还整了半天...) 代码: #include<c ...
随机推荐
- ContextLoaderListener作用详解(转)
ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在 ...
- Relevance Between Variable Declaration and Definition in C++
A declaration makes a name known to a programm. A definition creates the assocatied entity. A variab ...
- SDUT2241计算组合数C(n,m)(组合数)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2241 这个题的代码适应性也挺强,但这个题倒不适 ...
- hdu 4282 A very hard mathematic problem
由于k的范围是0-2^31,而且x,y,z都是正整数,由题易知道2<=z<31,1<=x<y;所以直接枚举就好了!!! #include<iostream> #in ...
- Swift入门(十一)——类型转换与is、as操作
三种操作:is.as?和as! Swift是强类型语言,但也允许开发者通过is.as?和as!这三种操作来对类型进行判断和强制转换.其中is用作类型判断,而as?和as!则分别是类型转换的可选形式和强 ...
- 汇编debug 截图
- Qt 添加外部库文件(四种方法)
Qt添加外部库文件, 一种就是直接加库文件的绝对路劲,这种方法简单,但是遇到多个库文件的时候,会很麻烦,而且,如果工程移动位置以后还需要重新配置 另一种就是相对路径了,不过Qt 编译的文件会在一个单独 ...
- Photoshop:建议设置
一.新建文档设置: 二.对齐设置 菜单->视图->对齐->全部 使用图层.形状等操作时自动对齐网格,画矢量图不怕模糊边缘,确保每个像素保持清晰. 三.首选项设置 关掉"启用 ...
- 258. Add Digits
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- 39. Combination Sum
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...