Poj 3301 Texas trip (three points)

Source: Internet
Author: User
Tags cos
Poj 3301 Texas trip (three points)

ACM

Question address:
Poj 3301 Texas trip

Question:
Given N points in a two-dimensional plane, a square with the smallest area is required to cover all points.

Analysis:
If you want to solve the problem, you will lose...
We can make the square do not move, and all vertices are rotated and transformed, so that the result will not be deformed.
Deformation means:x1=x*cos(a)-y*sin(a); y1=x*sin(a)+y*cos(a) 
I wanted to see it in a violent way. Later I found that it was three points, which proved to be a huge reference:

It can be proved that they are all convex functions, so the maximum difference between them is also a convex function. Therefore, we can use the three-way method to divide the Rotation Angle and find the smallest square that meets the requirements. For detailed procedures, see the code.

After sending a few rounds, I verified again that the double output of G ++ uses % F by default.

Code:

/**  Author:      illuz <iilluzen[at]gmail.com>*  File:        3301.cpp*  Create Date: 2014-09-18 09:25:04*  Descripton:  triple*/#include <cstdio>#include <cstring>#include <cmath>#include <iostream>#include <algorithm>using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)typedef long long ll;const int N = 31;const double PI = acos(-1.0);const double EPS = 1e-12;int t, n;double x[N], y[N];inline double calc(double a) {double minx = 1000, miny = 1000, maxx = -1000, maxy = -1000;double xx, yy;repf (i, 1, n) {xx = x[i] * cos(a) - y[i] * sin(a);yy = x[i] * sin(a) + y[i] * cos(a);minx = min(minx, xx);maxx = max(maxx, xx);miny = min(miny, yy);maxy = max(maxy, yy);}return max(maxx - minx, maxy - miny);}int main() {ios_base::sync_with_stdio(0);double l, r, mid, mmid, ans;cin >> t;while (t--) {cin >> n;repf (i, 1, n) {cin >> x[i] >> y[i];}l = 0.0;r = PI;while (r - l > EPS) {mid = (l + r) / 2;mmid = (mid + r) / 2;ans = calc(mid);if (ans <= calc(mmid))r = mmid;elsel = mid;}printf("%.2f\n", ans * ans);// brute force//ans = 1000;//for (double i = 0.0; i <= PI; i += 0.00005)//ans = min(ans, calc(i));//printf("%.2lf\n", ans * ans);}return 0;}


Poj 3301 Texas trip (three points)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.