POJ 2187 Beauty Contest(凸包暴力法)

來源:互聯網
上載者:User
Beauty Contest
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 24124   Accepted: 7364

Description

Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their
cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates (x,y), each having a value in the range -10,000 ... 10,000. No two farms share the same pair of coordinates. 

Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms can be quite large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. Since Bessie refills
her suitcase at every farm she visits, she wants to determine the maximum possible distance she might need to travel so she knows the size of suitcase she must bring.Help Bessie by computing the maximum distance among all pairs of farms. 

Input

* Line 1: A single integer, N 

* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm 

Output

* Line 1: A single integer that is the squared distance between the pair of farms that are farthest apart from each other. 

Sample Input

40 00 11 11 0

Sample Output

2

Hint

Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2) 

Source

USACO 2003 Fall

求出凸包上的點,然後暴力枚舉

開始過不了,後來把如果點的個數在100以內,就直接暴力A了!

#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <cmath>using namespace std;#define eps 1e-8#define PI 3.14159265struct point{int x;int y;}po[55500],temp;int n,pos;bool zero(double a){return fabs(a) < eps;}int dis(point &a,point &b)//返回兩點之間距離的平方{return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);}int across(point &a,point &b,point &c)//求a b and a c 的X積{return (b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x);}int cmp(const void *a,const void *b){return across(po[0],*(point*)a,*(point*)b) > 0 ? -1 : 1;}int select(){int i,j,k=1;for(i=2;i<n;i++){if(across(po[0],po[k],po[i])==0){if(dis(po[0],po[k]) < dis(po[0],po[i]))po[k]=po[i];}elsepo[++k]=po[i];}return k+1;}int graham(int num){int i,j,k=2;//////////////////////////////////////////po[num]=po[0];//fangbian num++;int ans=0;int t;if(num<20){for(i=0;i<num;i++)for(j=0;j<num;j++){t=dis(po[i],po[j]);if(t > ans)ans=t;}printf("%d\n",ans);return 0;}for(i=3;i<num;i++){while(across(po[k-1],po[k],po[i]) < -eps){k--;}po[++k]=po[i];//就這個迴圈結束,不需要了!}/*for(i=0;i<k;i++)printf("%lf %lf\n",po[i].x,po[i].y);*/for(i=0;i<k;i++)for(j=0;j<k;j++){t=dis(po[i],po[j]);if(t > ans)ans=t;}printf("%d\n",ans);return 0;}int main(){int i,j,k;point my_temp;int ans;int t;while(scanf("%d",&n)!=EOF){ans=0;scanf("%d%d",&po[0].x,&po[0].y);temp=po[0];pos=0;for(i=1;i<n;i++){scanf("%d%d",&po[i].x,&po[i].y);if(po[i].y < temp.y)temp=po[i],pos=i;}if(n<100){for(i=0;i<n;i++)for(j=0;j<n;j++){t=dis(po[i],po[j]);if(t > ans)ans=t;}printf("%d\n",ans);continue;}my_temp=po[0];po[0]=po[pos];po[pos]=my_temp;qsort(po+1,n-1,sizeof(po[0]),cmp);graham(select());}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.