11243  Texas Trip
Moderator: Board moderators
11243  Texas Trip
can someone please give a hint on this problem?
Thank you
Thank you
Ok, I'll give a hint on how I did it:
1. obviously the side of the minimum enclosing square must all lie outside or touch the convex hull.
2. at least 3 sides of the square will touch either a vertex of the convex hull or a side of convex hull.
3. Minimizing the area of the square is same as maximizing the length of the side of the square.
More hints:
4. for angles 0<=t<pi/2, define f(t) as the length of the side of minimum enclosing square with 2 sides in the direction of angle t from the xaxis.
5. Given t, to compute f(t) requires some linear algebra. Basically we want to rotate all the vertices of convex hull by angle t and find the maximum and minimum x,y values, and return max(ymaxymin,xmaxxmin)
6. Now, we need to compute the minimum f(t) over all 0<=t<pi/2.
Figure out how to do it efficiently.
7. Although I haven't proved it yet, the critical points of f(t) either corresponds to angles t in the directions of sides of convex hull, or the local minimas.
8. Use ternary search to find the minimum f(t) between each critical angles t1,t2.
PS: instead of angle t, we can also use vectors to parametrize the function f, leading to better accuracy.
1. obviously the side of the minimum enclosing square must all lie outside or touch the convex hull.
2. at least 3 sides of the square will touch either a vertex of the convex hull or a side of convex hull.
3. Minimizing the area of the square is same as maximizing the length of the side of the square.
More hints:
4. for angles 0<=t<pi/2, define f(t) as the length of the side of minimum enclosing square with 2 sides in the direction of angle t from the xaxis.
5. Given t, to compute f(t) requires some linear algebra. Basically we want to rotate all the vertices of convex hull by angle t and find the maximum and minimum x,y values, and return max(ymaxymin,xmaxxmin)
6. Now, we need to compute the minimum f(t) over all 0<=t<pi/2.
Figure out how to do it efficiently.
7. Although I haven't proved it yet, the critical points of f(t) either corresponds to angles t in the directions of sides of convex hull, or the local minimas.
8. Use ternary search to find the minimum f(t) between each critical angles t1,t2.
PS: instead of angle t, we can also use vectors to parametrize the function f, leading to better accuracy.
Last edited by sclo on Tue Jul 17, 2007 8:15 am, edited 3 times in total.
I think you are making this problem too complicated. Convex Hull is not needed at all. I did it just using the concept of rotating all the points by an angle 0<=t<pi/2. That's 90*. With the points rotated, you can find the minx, maxx, miny, maxx and the smallest square will have the side max(maxxminx, maxyminy). No convex hull needed. And like it was said already, the minumum of this function f(t) is needed. To find the minimum of the function, you can use tertiary search, but you have to be careful, since there may be local minimums, as well as the absolute minumum. As for the details on how to take care of that, you have to figure it out. My solution is the fastest right now at 0.012 seconds, but I don't think it will hold for much longer.
convex hull is required for the proofs that my ternary search will in fact find the solution. I.e. the function is either concave up, or linear.
I believe that this method is called rotating caliper.
PS: my method is O(nlogn + nD) where D is the number of iterations that I use in the ternary search. I just use D=100.
It is also clear how to derive O(n^2*logn + n^2*D) solution from my solution that is much much easier.
I believe that this method is called rotating caliper.
PS: my method is O(nlogn + nD) where D is the number of iterations that I use in the ternary search. I just use D=100.
It is also clear how to derive O(n^2*logn + n^2*D) solution from my solution that is much much easier.

 New poster
 Posts: 9
 Joined: Sat Jul 07, 2007 5:47 pm
 Location: Denmark

 Experienced poster
 Posts: 109
 Joined: Sat Jun 23, 2007 9:53 pm
 Location: Brest, BELARUS
 Contact:
and why do I get WA then?? I rotate points...
Code: Select all
#include <cstdio>
#include <math.h>
#include <iostream>
#include <cstdlib>
const
double PI = 3.14159265358979323846;
using namespace std;
void f(double &x,double &y,double beta) {
double xt,yt;
xt = x*cos(beta)  y*sin(beta);
yt = x*sin(beta) + y*cos(beta);
x = xt;
y = yt;
}
double abs2 (double n) {
return n<0?n:n;
}
double max2(double a, double b) {
return a>b?a:b;
}
int main(int argc, char* argv[]) {
int ci,cn;
int i,n;
double x[51],y[51],x1[51],y1[51];
double minx,maxx,miny,maxy,e1,ans,alpha;
cin >> cn;
for (ci=1;ci<=cn;ci++) {
cin >> n;
for (i=1;i<=n;i++)
cin >> x[i] >> y[i];
alpha = 0;
for (i=1;i<=n;i++) {
x1[i] = x[i];
y1[i] = y[i];
}
ans = 20000000;
while (alpha<=PI){
for (i=1;i<=n;i++) {
x[i] = x1[i];
y[i] = y1[i];
}
minx = 2000000;
maxx = minx;
miny = minx;
maxy = minx;
for (i=1;i<=n;i++) {
f(x[i],y[i],alpha);
if (x[i]<minx)
minx = x[i];
if (x[i]>maxx)
maxx = x[i];
if (y[i]<miny)
miny = y[i];
if (y[i]>maxy)
maxy = y[i];
}
e1 = max2(abs2(minxmaxx),abs2(minymaxy));
e1 = e1*e1;
if (e1<ans)
ans = e1;
alpha = alpha + PI/2000.0;
}
printf("%.2f\n",ans+0.00001);
}
return 0;
}
Now I lay me down to sleep...
my profile
my profile

 Experienced poster
 Posts: 109
 Joined: Sat Jun 23, 2007 9:53 pm
 Location: Brest, BELARUS
 Contact:
Well, I have read google and wiki about the Ternary Search, but please explain me the use of it in this task. In pseudo code this algo looks nearly this way:
But what is the function "f" in our task? I cannot understand.
Thanx in advance.
Code: Select all
function ternarySearch(f, left, right, absolutePrecision)
//left and right are the current bounds; the maximum is between them
if (rightleft < absolutePrecision) return (left+right)/2
leftThird := (left*2+right)/3
rightThird := (left+right*2)/3
if (f(leftThird) < f(rightThird))
return ternarySearch(f, leftThird, right, absolutePrecision)
else
return ternarySearch(f, left, rightThird, absolutePrecision)
end
Thanx in advance.
Now I lay me down to sleep...
my profile
my profile

 Experienced poster
 Posts: 122
 Joined: Sun Nov 13, 2005 10:25 am
 Location: Taiwan
Re: 11243  Texas Trip
Hi
Can anybody explain correctness of problem sample input.
Why the square for this case is 242. I believe that it has to be 40. Where is my mistake?
4
10 1
10 1
10 1
10 1
Can anybody explain correctness of problem sample input.
Why the square for this case is 242. I believe that it has to be 40. Where is my mistake?
4
10 1
10 1
10 1
10 1

 Guru
 Posts: 5947
 Joined: Thu Sep 01, 2011 9:09 am
 Location: San Jose, CA, USA
Re: 11243  Texas Trip
To cover the holes with an area of 40 you'd have to use a rectangle not a square.
Check input and AC output for thousands of problems on uDebug!