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个后面。

 1 //#define LOCAL
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <vector>
 7 using namespace std;
 8
 9 const double PI = acos(-1.0);
10
11 struct Point
12 {
13     double x, y;
14     Point(double x=0, double y=0):x(x), y(y){}
15 };
16 typedef Point Vector;
17 Point operator + (Vector A, Vector B) { return Vector(A.x+B.x, A.y+B.y); }
18 Point operator - (Vector A, Vector B) { return Vector(A.x-B.x, A.y-B.y); }
19 double Cross(const Vector& A, const Vector& B)      { return A.x*B.y - A.y*B.x; }
20 Vector Rotate(Vector A, double p)
21 {
22     return Vector(A.x*cos(p)-A.y*sin(p), A.x*sin(p)+A.y*cos(p));
23 }
24 bool operator < (const Point& A, const Point& B)
25 {
26     return A.x < B.x || (A.x == B.x && A.y < B.y);
27 }
28 bool operator == (const Vector& A, const Vector& B)
29 {
30     return A.x == B.x && A.y == B.y;
31 }
32
33 double torad(double x) { return x / 180.0 * PI; }
34
35 vector<Point> ConvexHull(vector<Point> p)
36 {
37     //Ô¤´¦Àí£¬È¥ÖØ
38     sort(p.begin(), p.end());
39     p.erase(unique(p.begin(), p.end()), p.end());
40
41     int n = p.size();
42     int m = 0;
43     vector<Point> ch(n+1);
44     for(int i = 0; i < n; ++i)
45     {
46         while(m > 1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
47         ch[m++] = p[i];
48     }
49     int k = m;
50     for(int i = n-2; i >= 0; --i)
51     {
52         while(m > k && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
53         ch[m++] = p[i];
54     }
55     if(n > 1)    m--;
56     ch.resize(m);
57     return ch;
58 }
59
60 double PolygonArea(vector<Point> p)
61 {
62     int n = p.size();
63     double ans = 0.0;
64     for(int i = 0; i < n-1; ++i)
65             ans += Cross(p[i]-p[0], p[i+1]-p[0]);
66     return ans/2;
67 }
68
69 int main(void)
70 {
71     #ifdef LOCAL
72         freopen("10652in.txt", "r", stdin);
73     #endif
74
75     int T;
76     scanf("%d", &T);
77     while(T--)
78     {
79         int n;
80         vector<Point> p;
81         double area1 = 0.0;
82         scanf("%d", &n);
83         for(int i = 0; i < n; ++i)
84         {
85             double x, y, w, h, a;
86             scanf("%lf%lf%lf%lf%lf", &x, &y, &w, &h, &a);
87             Point o(x, y);
88             double ang = -torad(a);
89             p.push_back(o + Rotate(Vector(w/2, h/2), ang));
90             p.push_back(o + Rotate(Vector(-w/2, h/2), ang));
91             p.push_back(o + Rotate(Vector(w/2, -h/2), ang));
92             p.push_back(o + Rotate(Vector(-w/2, -h/2), ang));
93             area1 += w*h;
94         }
95         double area2 = PolygonArea(ConvexHull(p));
96         printf("%.1lf %%\n", area1 / area2 * 100.0);
97     }
98 }

代码君

时间: 2024-07-29 00:42:36

UVa 10652 (简单凸包) Board Wrapping的相关文章

uva 10652 Board Wrapping (计算几何-凸包)

Problem B Board Wrapping Input: standard input Output: standard output Time Limit: 2 seconds The small sawmill in Mission, British Columbia, has developed a brand new way of packaging boards for drying. By fixating the boards in special moulds, the b

Uva 10652 Board Wrapping

二次联通门 : Virtual Judge Uva 10652 Board Wrapping /* Uva 10652 Board Wrapping 计算几何 将每个家具拆为4个点 对所有点求一个凸包 然后求凸包的面积即可 思路不难.. 但是写起来好麻烦好麻烦 需要注意的东西有很多 1. 角度制转弧度制 2. EPS的选择 3. PI的取值 (以防万一以后还是都写acos(-1)吧...) 4. 在把家具拆成四个点时的细节 4. 计算多边形的面积 */ #include <algorithm>

UVA 10652 Board Wrapping 计算几何

多边形凸包.... Board Wrapping Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem B Board Wrapping Input: standard input Output: standard output Time Limit: 2 seconds The small sawmill in Mission, Britis

Board Wrapping(计算几何求凸包加向量的旋转)

UVA - 10652 Board Wrapping Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Submit]   [Go Back]   [Status] Description Problem B Board Wrapping Input: standard input Output: standard output Time Limit: 2 seconds The small sa

uva 10065 (凸包+求面积)

链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=1006 Problem A Useless Tile Packers Input: standard input Output: standard output Yes, as you have apprehended the Useless Tile Pac

UVa 11168 Airport , 凸包

题意: 给出平面上n个点,找一条直线,使得所有点在直线的同侧,且到直线的距离之平均值尽量小. 先求凸包 易知最优直线一定是凸包的某条边,然后利用点到直线距离公式进行计算. #include<cstdio> #include<cstring> #include<vector> #include<cmath> #include<algorithm> #include<iostream> using namespace std; struc

UVa 11178 (简单练习) Morley&#39;s Theorem

题意: Morley定理:任意三角形中,每个角的三等分线,相交出来的三个点构成一个正三角形. 不过这和题目关系不大,题目所求是正三角形的三个点的坐标,保留6位小数. 分析: 由于对称性,求出D点,EF也是同样的. 用点和向量的形式表示一条直线,向量BA.BC的夹角为a1,则将BC逆时针旋转a1/3可求得 直线BD,同理也可求得直线CD,最后再求交点即可. 1 //#define LOCAL 2 #include <cstdio> 3 #include <cstring> 4 #in

【简单凸包】LightOJ 1203 Guarding Bananas

[简单凸包]LightOJ 1203 Guarding Bananas 题目链接:LightOJ 1203 Guarding Bananas 题目大意 构造凸包,求凸包夹角的最小值 笔者的第一道凸包题目,发现Kuangbin的计算几何模板的一个最大缺陷:结构体太长,大空间开不下QAQ 凸包,我的理解是包含已知点集的最小凸集,二维凸包自然可以理解为包含所有点的最小凸多边形. 现在代码的逼格越来越高了~(≧▽≦)/~啦啦啦! 说一下思路 ①Graham's Scan法构建凸包,时间复杂度O(nlog

简单几何(向量旋转+凸包+多边形面积) UVA 10652 Board Wrapping

题目传送门 题意:告诉若干个矩形的信息,问他们在凸多边形中所占的面积比例 分析:训练指南P272,矩形面积长*宽,只要计算出所有的点,用凸包后再求多边形面积.已知矩形的中心,向量在原点参考点再旋转,角度要转换成弧度制. /************************************************ * Author :Running_Time * Created Time :2015/11/10 星期二 10:34:43 * File Name :UVA_10652.cpp