poj 3301 Texas Trip 三分搜索
2015-07-22 10:58
387 查看
Texas Trip
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
Sample Output
题意:平面上给出一些点,求出最小包含所有点的正方形的面积。
思路:要求最小的正方形包含所有的点,一个简单的思路就是先求出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;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4459 | Accepted: 1375 |
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
2 4 -1 -1 1 -1 1 1 -1 1 4 10 1 10 -1 -10 1 -10 -1
Sample Output
4.00 242.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;
}
相关文章推荐
- Android横竖屏切换
- 函数返回值为数组
- ajax 后台正常执行,前台报错信息却是404
- android自定义加载框
- Ubuntu设置root用户登录
- OpenGL中各种坐标系的理解
- bootstrap css语法,javascript
- 【飞控学习1】APM和PX4飞控源码下载及安装
- 背包九讲
- undefined reference to: xxx
- 标准输入输出重定向
- 在线学习为course扩充字段
- adb 常用命令
- mac 中安装git
- Scripting.FileSystemObject控件的用法
- 安卓视图动画+收缩与展开
- nyoj19擅长排列的小明(递归)
- ueditor 后端配置项没有正常加载,上传插件不能正常使用!jsp版本
- String a=new String("b","c")会创建几个对象?
- 3Sum Closest (leetcode 16)