poj 3301 Texas Trip 三分搜索
来源:互联网 发布:潜水教练 知乎 编辑:程序博客网 时间:2024/06/05 15:43
Description
After a day trip with his friend Dick, Harry noticed a strange pattern of tiny holes in the door of his SUV. The local American Tire store sells fiberglass patching material only in square sheets. What is the smallest patch that Harry needs to fix his door?
Assume that the holes are points on the integer lattice in the plane. Your job is to find the area of the smallest square that will cover all the holes.
Input
The first line of input contains a single integer T expressed in decimal with no leading zeroes, denoting the number of test cases to follow. The subsequent lines of input describe the test cases.
Each test case begins with a single line, containing a single integer n expressed in decimal with no leading zeroes, the number of points to follow; each of the following n lines contains two integers x and y, both expressed in decimal with no leading zeroes, giving the coordinates of one of your points.
You are guaranteed that T ≤ 30 and that no data set contains more than 30 points. All points in each data set will be no more than 500 units away from (0,0).
Output
Print, on a single line with two decimal places of precision, the area of the smallest square containing all of your points.
Sample Input
24-1 -11 -11 1-1 1410 110 -1-10 1-10 -1
Sample Output
4.00242.00题意:平面上给出一些点,求出最小包含所有点的正方形的面积。
思路:要求最小的正方形包含所有的点,一个简单的思路就是先求出x,y的最大、最小值做一个初始正方形然后开始旋转,旋转过程中便会出现一个最小的面积包含所有的点。但是旋转正方形并改变边长也并不容易,所以就让正方形不动,旋转点,对于每个旋转角度,求出x,y最大最小值,这就转化为一个三分搜索求极值的问题(对于旋转过程中正方形面积变化为什么是旋转角度凸函数我就不知道了。。。)。所以最终三分旋转角度,求出面积极值。。
代码:
#include <cstdio>#include <cmath>#include <algorithm>using namespace std;#define maxn 50#define inf 0x3fffffff#define pi acos(-1.0)struct Point{double x,y;}p[maxn];int n;double f(double a){double minx=inf,maxx=-inf,miny=inf,maxy=-inf;double tx,ty;for(int i=0;i<n;i++){tx = p[i].x*cos(a) - p[i].y*sin(a);ty = p[i].y*cos(a) + p[i].x*sin(a);if(tx>maxx) maxx = tx;if(tx<minx) minx = tx;if(ty>maxy) maxy = ty;if(ty<miny) miny = ty;}return max(maxx-minx,maxy-miny);}int main(){int T;scanf("%d",&T);while(T--){scanf("%d",&n);for(int i=0;i<n;i++){scanf("%lf %lf",&p[i].x,&p[i].y);}double l=0.0, r = pi,ll,rr;for(int i=0;i<100;i++){ll = l+(r-l)/3;rr = r-(r-l)/3;if(f(ll)<f(rr)) r = rr;else l = ll;}printf("%.2lf\n",f(ll)*f(ll));}return 0;}
- Poj 3301 Texas Trip (三分搜索)
- poj 3301 Texas Trip 三分搜索
- poj 3301 Texas Trip 三分
- POJ 3301 Texas Trip(三分)
- POJ 3301 Texas Trip(三分)
- 三分 --- POJ 3301 Texas Trip
- 【POJ 3301】 三分 Texas Trip
- Texas Trip+POJ+三分
- poj 3301 Texas Trip (三分求极值)
- POJ 3301 Texas Trip(三分枚举)
- POJ 3301 Texas Trip (三分)
- poj 3301 Texas Trip(旋转+三分)
- POJ 3301 Texas Trip(三分求最小值)
- POJ 3301 Texas Trip(三分)
- poj 3301 Texas Trip (三分)
- POJ 3301 Texas Trip (三分求极限)
- POJ 3301 Texas Trip[三分]
- POJ 3301 Texas Trip(最小正方形覆盖+三分)
- Target runtime com.genuitec.runtime.generic.jee60 is not defined的解决方案
- MSTSC 通过CONSOLE远程登陆
- C#中如何简单的设置session的过期时间
- 【Java】编写一个方法,对字符串数组进行排序,将所有变位词排在相邻的位置
- 如何使用ListView?
- poj 3301 Texas Trip 三分搜索
- 12. JavaScript Number 对象
- The connection to adb is down, and a severe error has occured.问题解决方法小结
- 欢迎使用CSDN-markdown编辑器
- Linux下文件操作的基本函数及分析
- SPOJ 375 Query on a tree (树链剖分+线段树)
- 使用Github依赖库实现Android5.0新特性——CardView
- IE8判断
- 2015多校第一场 hdu 5288 OO’s Sequence