2015baidu复赛 矩形面积(包凸 && ps:附quickhull模板)
2015-06-02 01:46
453 查看
矩形面积
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 168 Accepted Submission(s): 93
[align=left]Problem Description[/align]
小度熊有一个桌面,小度熊剪了很多矩形放在桌面上,小度熊想知道能把这些矩形包围起来的面积最小的矩形的面积是多少。
[align=left]Input[/align]
第一行一个正整数 T,代表测试数据组数(1≤T≤20),接下来 T 组测试数据。
每组测试数据占若干行,第一行一个正整数 N(1≤N<≤1000),代表矩形的数量。接下来 N 行,每行 8 个整数x1,y1,x2,y2,x3,y3,x4,y4,代表矩形的四个点坐标,坐标绝对值不会超过10000。
[align=left]Output[/align]
对于每组测试数据,输出两行:
第一行输出"Case #i:",i 代表第 i 组测试数据。
第二行包含1 个数字,代表面积最小的矩形的面积,结果保留到整数位。
[align=left]Sample Input[/align]
2
2
5 10 5 8 3 10 3 8
8 8 8 6 7 8 7 6
1
0 0 2 2 2 0 0 2
[align=left]Sample Output[/align]
Case #1:
17
Case #2:
4
[align=left]Source[/align]
2015年百度之星程序设计大赛 - 初赛(1)
#include<bits/stdc++.h> #define one first #define two second #define eps 1e-12 #define sgn(x) (x<eps?0:(x>0?1:-1)) #define cross(a,b,c) ((b.one-a.one)*(c.two-a.two)-(b.two-a.two)*(c.one-a.one)) #define cmp(a,b) (a.one<b.one || sgn(a.one-b.one)==0 && a.two<b.two) const int M = 1e5 , inf = 0x3f3f3f3f ; int n ; std::pair<double , double> p [M] ; int s[M] ; std::pair<double , double> jk[M] ; int tot = 0 ; void hull (int l , int r , std::pair<double , double> a , std::pair<double , double> b) { //printf ("(%d , %d)\n" , l , r ) ; int x = l , _l = l - 1 , _r = r + 1 ; for (int i = l ; i <= r ; i ++) { int tmp = sgn(s[i]-s[x]) ; if (tmp > 0 || tmp == 0 && cmp(p[x],p[i])) x = i ; } for (int i = l ; i <= r ; i ++) { s[++ _l] = cross(p[i],a,p[x]) ; if (sgn(s[_l]) > 0) std::swap (p[i] , p[_l]) ; else _l -- ; } for (int i = r ; i >= l ; i --) { s[-- _r] = cross(p[i],p[x],b) ; if (sgn(s[_r]) > 0) std::swap (p[i] , p[_r]) ; else _r ++ ; } if (l <= _l)hull (l , _l , a , p[x]) ; jk[tot ++] = p[x] ; if (_r <= r)hull (_r , r , p[x] , b) ; } int main () { // freopen ("a.txt" , "r" , stdin ) ; memset (s , 0 , sizeof(s)) ; scanf ("%d" , &n) ; //printf ("n = %d\n" , n ) ; int x = -1 ; for (int i = 1 ; i <= n ; i ++) { scanf ("%lf%lf" , &p[i].one , &p[i].two) ; if (x == -1 || cmp (p[i] , p[x])) x = i ; } std::swap (p[x] , p[1]) ; jk[tot ++] = p[1] ; hull (2 , n , p[1] , p[1]) ; for (int i = 0 ; i < tot ; i ++) printf ("(%.4f , %.4f)\n" , jk[i].one , jk[i].two) ; return 0 ; }
92_quickhull
要了解这个算法的思路,先去维基上看看动态演示http://en.wikipedia.org/wiki/Quickhull
然后你需要知道叉积(叉积 "X" 表示):已知两个向量 a = (x1 , y1) , b = (x2 , y2);
叉积:a X b = x1 * y2 - x2 * y1 ;
点积:a * b = x1 * x2 + y1 * y2 ;
两个向量间的的夹角:a * b = |a| * |b| * cosθ ;
那么这两个向量 构成的三角形面积 S = 1/2 * abs (a X b) ,
还有 a X b = - b X a ;
知道以上这两个概念,就看的动quickhull了。
就这道题而言,你还需要知道, a 在 b 上的投影求法:a * b / | b | ;
相关文章推荐
- mysql插入中文时报错:incorrect string value
- easyUI的iframe子页面操作父页面元素
- hdu 2544最短路——最短路的初次总结 UESTC 6th Programming Contest Online
- 解读上海远丰MallBuilder商城系统的功能特性
- 自定义Cell的步骤
- easyUI的iframe子页面操作父页面元素
- java DelayQueue 延期队列 21.7.3 -------thinking java 4
- C# - GUID
- iOS AFNetworking “Request failed: unacceptable content-type: text/html”问题
- 为什么很多内置类如UITableViewController的delegate属性都是assign而不是retain的?
- Android Studio导入uiautomator测试框架
- iOS开发 --制作圆形的头像(UIImage)
- hdu 1711 Number Sequence KMP模板题
- Maximum Subsequence Sum
- iOS UIview 学习
- SSDB --- queue实现
- IOS初级:UIwindow
- android蓝牙框架bluedroid之sbc编码
- 【Android UI设计与开发】第02期:引导界面(二)使用ViewPager实现欢迎引导页面
- UIView UIWindow