POJ 3301 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
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
給定n個點的座標求一個最小的正方形使n個點都在正方形內
先假定邊長最小且個邊都平行於座標軸的正方形,然後三分旋轉的角度
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const double eps = 1e-12;
const double INF = 1e6;
const double PI = acos(-1.0);
double p[35][2];
int n;
double cir(double x){//旋轉x角度後對應點的位置
double minx,miny,maxx,maxy;
double tmpx,tmpy;
minx=miny=INF;
maxx=maxy=-INF;
for(int i=0;i<n;i++){
tmpx=p[i][0]*cos(x)-p[i][1]*sin(x);
tmpy=p[i][1]*cos(x)+p[i][0]*sin(x);
minx=min(tmpx,minx);
miny=min(tmpy,miny);
maxx=max(tmpx,maxx);
maxy=max(tmpy,maxy);
}
return max(maxx-minx,maxy-miny);
}
double ternary_search(double l,double r){//三分旋轉的角度
double ll,rr;
while(r-l>eps){
ll=(l*2+r)/3;
rr=(l+r*2)/3;
if(cir(ll)>cir(rr))
l=ll;
else
r=rr;
}
return l;
}
int main()
{
int cas;
cin>>cas;
while(cas--){
cin>>n;
for(int i=0;i<n;i++)
cin>>p[i][0]>>p[i][1];
double l=0,r=PI/2;
double tmp=ternary_search(l,r);
printf("%.2lf\n",cir(tmp)*cir(tmp));
}
return 0;
}
相關文章
- POJ 1734 Sightseeing trip Floyd求無向圖最小環
- CF1925D Good TripGo
- ABC 320F Fuel Round Trip
- BZOJ1395 : [Baltic2005]Trip
- CodeForces703B Mishka and trip 數學
- JavaScript’s “this”: how it works, where it can trip you upJavaScript
- [USACO23DEC] Minimum Longest Trip G3D
- Schemaless架構(二):Uber的Trip資料庫架構資料庫
- POJ 3461 kmpKMP
- poj3417
- SAP CRM Fiori應用冗餘round trip的原因分析
- 懂你英語L4 Unit1 3 Harry's trip
- Schemaless架構(三):Uber基於MySQL的Trip資料庫架構MySql資料庫
- Flip Game(POJ 1753)GAM
- POJ 2431 Expedition
- POJ3259-WormholesWorm
- POJ 3414 Pots
- SiteMinder:全球酒店預訂渠道排名:Airbnb、Trip.com引關注AI
- A trip through the graphics pipeline 2011 Part 10(翻譯)
- POJ 1562 Oil Deposits
- poj 2478 尤拉函式函式
- POJ 2524 Ubiquitous ReligionsUI
- POJ 3981 字串替換字串
- poj 1276 Cash MachineMac
- POJ 基本演算法演算法
- POJ3414-Pots
- POJ 1089 Intervals
- 英語寫作 第六天:A TRIP TO THE COUNTRY 鄉村遊記
- 三分鐘搞懂桶排序排序
- 模板 - 二分&三分
- POJ 2799 IP Networks
- POJ 2891 Strange Way to Express IntegersExpress
- Dungeon Master(POJ-2251)AST
- 【poj3468】A Simple Problem with Integers
- POJ 3321 Apple TreeAPP
- POJ 1160 Post Office
- POJ-2251 Dungeon MasterAST
- poj 2481 樹狀陣列陣列