-
Notifications
You must be signed in to change notification settings - Fork 3
/
10173-Smallestboundingrectangle.cpp
executable file
·110 lines (91 loc) · 1.67 KB
/
10173-Smallestboundingrectangle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// 10173 - Smallest bounding rectangle.cpp
// Uva
//
// Created by Alexander Faxå on 2012-05-19.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#include <iostream>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <vector>
#define PI acos(-1)
#define INF 1e10
using namespace std;
struct point {
double x, y;
};
int main()
{
int n;
while(true)
{
cin >> n;
if(!n)
break;
vector<point>unm;
double b[1002];
double z[1002];
double xmin = INF;
for(int i = 0; i < n; i++)
{
point p;
cin >> p.x >> p.y;
unm.push_back(p);
xmin = min(xmin, p.x);
}
if(xmin<0.5)
for(int i = 0; i < n; i++)
unm[i].x += 0.5-xmin;
for(int i = 0; i < n; i++)
{
point cur = unm[i];
b[i] = atan(cur.y/cur.x);
z[i] = sqrt(pow(cur.x,2)+pow(cur.y,2));
}
double res = INF;
double diff = 5;
double bestv = -5;
for(int k = 0; k < 12; k++)
{
diff /= 15;
double lower, upper;
if(bestv < -3)
{
lower = 0;
upper = PI/2;
}
else
{
lower = bestv - diff;
upper = bestv + diff;
}
for(double alfa = lower; alfa <= upper; alfa += diff/50)
{
double xmin = INF;
double xmax = -INF;
double ymin = INF;
double ymax = -INF;
for(int i = 0; i < unm.size(); i++)
{
point cur = unm[i];
cur.y = z[i]*sin(b[i]+alfa);
cur.x = z[i]*cos(b[i]+alfa);
xmin = min(xmin, cur.x);
xmax = max(xmax, cur.x);
ymin = min(ymin, cur.y);
ymax = max(ymax, cur.y);
}
double newvalue = fabs(xmin-xmax)*fabs(ymin-ymax);
if(newvalue < res)
{
res = newvalue;
bestv = alfa;
}
}
}
printf("%.4f\n", res);
}
return 0;
}