POJ 3301:Texas Trip(计算几何+三分)

http://poj.org/problem?id=3301

题意:在二维平面上有n个点,每个点有一个坐标,问需要的正方形最小面积是多少可以覆盖所有的点。

思路:从第二个样例可以看出,将正方形旋转45°的时候,面积是最小的。

因此考虑旋转正方形,就可以当作旋转本来的点,对于旋转后的点,求最大的x和最小的x,最大的y和最小的y,就可以求得覆盖旋转后的点的正方形面积了。

然后对于每一个角度,都要进行判断,这个时候就觉得要用到X分了。

因为不满足单调性,所以用了三分。(其实也不太清楚为什么能三分)。

因为要求最小,因此是凹形的。

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cmath>
 5 using namespace std;
 6 #define N 40
 7 #define INF 0x3f3f3f3f
 8 const double eps = 1e-9;
 9 const double PI = acos(-1.0);
10 struct node {
11     double x, y;
12 } p[N];
13 int n;
14
15 double cal(double ang) {
16     double ix = 1000000000, iy = 1000000000, ax = -1000000000, ay = -1000000000;
17     for(int i = 1; i <= n; i++) {
18         double x = p[i].x * cos(ang) - p[i].y * sin(ang);
19         double y = p[i].x * sin(ang) + p[i].y * cos(ang);
20         ix = min(ix, x);
21         iy = min(iy, y);
22         ax = max(ax, x);
23         ay = max(ay, y);
24     }
25     return max(ax - ix, ay - iy);
26 }
27
28 void Solve() {
29     double l = 0, r = PI;
30     while(fabs(r - l) > eps) {
31         double mid = (l + r) / 2;
32         double midd = (mid + r) / 2;
33         if(cal(mid) <= cal(midd)) r = midd;
34         else l = mid;
35     }
36     double ans = cal(l);
37     printf("%.2f\n", ans * ans);
38 }
39
40 int main() {
41     int t; scanf("%d", &t);
42     while(t--) {
43         scanf("%d", &n);
44         for(int i = 1; i <= n; i++) scanf("%lf%lf", &p[i].x, &p[i].y);
45         Solve();
46     }
47     return 0;
48 }
时间: 2024-12-19 12:59:39

POJ 3301:Texas Trip(计算几何+三分)的相关文章

POJ 3301 Texas Trip (三分)

题目链接 题意 : 给你若干个点,让你找最小的正方形覆盖这所有的点.输出面积. 思路 : 三分枚举正方形两对边的距离,然后求出最大,本题用的是旋转正方形,也可以用旋转点,即点的相对位置不变. 正方形从0度到180度变化的过程中,把所有点覆盖,面积肯定是有一个最小峰值,是一个凸函数.因此可以用三分法解决.这里有一个难点就是已知两个定点的x,y坐标,过这两个点做两条平行线,平行线并与x轴成d度的角,怎么算出两条平行线的距离. d1 = fabs(cos(d)*(yi-yj)-sin(d)*(xi-x

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 nee

POJ 3301 Texas Trip

题目链接:http://poj.org/problem?id=3301 Texas Trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4935 Accepted: 1543 Description After a day trip with his friend Dick, Harry noticed a strange pattern of tiny holes in the door of his SUV. The

三分 --- POJ 3301 Texas Trip

Texas Trip Problem's Link:   http://poj.org/problem?id=3301 Mean: 给定n(n <= 30)个点,求出包含这些点的面积最小的正方形的面积. analyse: 首先要确定的是旋转的角度在0到180度之间即可,超过180度是和前面的相同的. 坐标轴旋转后,坐标变换为: X’ = x * cosa - y * sina; y’ = y * cosa + x * sina; Time complexity: O(n) Source code

poj3301 Texas Trip【三分算法】

题目地址:http://poj.org/problem?id=3301 简述:T组测试数据,每组线输入n,代表有n个点,接下来输入这n个点的坐标,坐标都是整数. 要求用一个最小的正方形覆盖所有的点,输出它的面积,精确到小数点后两位. 算法思路:枚举角度,计算面积, 三分枚举 (可参考:程序设计 解题策略  吴永辉...著 394页) 代码: #include <stdio.h> #include <string.h> #include <stdlib.h> #inclu

【POJ】2318 TOYS ——计算几何+二分

TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10281   Accepted: 4924 Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys away w

Poj 1556 The Doors 计算几何+最短路

其实本题非常的无脑,无脑拍完1A,写到blog里只因为TM无脑拍也拍了很久啊= = #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdl

POJ 3304 Segments(计算几何:直线与线段相交)

POJ 3304 Segments 大意:给你一些线段,找出一条直线能够穿过所有的线段,相交包括端点. 思路:遍历所有的端点,取两个点形成直线,判断直线是否与所有线段相交,如果存在这样的直线,输出Yes,但是注意去重. struct Point { double x, y; } P[210]; struct Line { Point a, b; } L[110]; double xmult(Point p1, Point p2, Point p) { return (p1.x-p.x)*(p2.

转载::POJ 2991 线段树+计算几何(有c++结构体操作)

POJ 2991 线段树+计算几何 (2011-02-27 21:13:44) 转载▼ 标签: 杂谈 分类: OI 话说这一题真的是很恶心很恶心,不过确实改变了我对线段树的一些看法,算是很经典的题目. 题意:有一个吊车由很多个不同长度的线段组成,一开始是一条长直线起点在(0,0),尾节点在(0,sum[n]),每条线段之间的夹角的初始值是180度.然后有一些操作a. b将第a条线段和a+1之间的夹角变成b度,经过每一次操作都要求出尾节点的坐标. 首先要用到一个计算几何的知识(没学过..请教而来)

POJ 2405 Beavergnaw (计算几何-简单题)

Beavergnaw Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6203   Accepted: 4089 Description When chomping a tree the beaver cuts a very specific shape out of the tree trunk. What is left in the tree trunk looks like two frustums of a co