poj 2187 Beauty Contest(凸包)
2015-08-18 19:50
363 查看
不知道这道题在比较的时候为什么用qsort()才能过,不过找到了一组数据
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
Sample Output
10 0 0 10000 0 1 100 2 199 9999 100 9998 199 100 -900 200 -1799 9800 -1799 9900 -900 答案应该是100000000
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
4 0 0 0 1 1 1 1 0
Sample Output
2
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define LL long long #define INF 10001 #define MAX 500100 using namespace std; struct node { int x,y; } ls[MAX]; int n; int Point[MAX]; int pos; node *p; int Dis(node a,node b) { return (b.x - a.x)*(b.x - a.x)+(b.y - a.y)*(b.y - a.y); } int dit(int x1,int y1,int x2,int y2) { return x1 * y2 - x2 * y1; } int Cross(node a,node b,node c,node d) { return dit(b.x-a.x, b.y-a.y, d.x-c.x, d.y-c.y); } int cmp(const void* a,const void* b) { node *s = (node*)a; node *t = (node*)b; int temp = Cross(*p,*s,*p,*t); if(temp>0) return -1; else if(temp==0) return Dis(*p,*t)-Dis(*p,*s); return 1; } int main() { // freopen("in.txt","r",stdin); std::ios::sync_with_stdio(false); while(cin>>n) { pos = 0; int MX = INF; for(int i=1;i<=n;i++) { cin>>ls[i].x>>ls[i].y; if(MX > ls[i].x) { pos = i; MX = ls[i].x; } else if(MX == ls[i].x) { if(ls[i].y < ls[pos].y) { pos = i; } } } swap(ls[pos],ls ); p=&ls ; qsort(ls+1,n,sizeof(node),cmp); int cnt = 2; int p = 2; Point[1] = n; Point[2] = 1; for(int i=2;i<=n;) { if(Cross(ls[Point[p-1]],ls[Point[p]],ls[Point[p]],ls[i]) >= 0) { Point[++p] = i++; cnt++; } else { p--; cnt--; } } int MAX_len = 0; for(int i=1;i<cnt-1;i++) { for(int j=i+1;j<cnt;j++) { int d = Dis(ls[Point[i]],ls[Point[j]]); if(d > MAX_len) MAX_len = d; } } cout<<MAX_len<<endl; } return 0; }
相关文章推荐
- MEDICAL EQUIPMENT OPENCART 自适应主题模板 ABC-0148
- HDU 2680 Choose the best route <迪杰斯特拉算法>
- Sudoku(POJ--2676
- [TOJ 1029] Robbery
- 浅谈 磁盘调度算法
- cocos2d-x开发日志08 —— 绘图工具
- [Asp.net]缓存之页面缓存,控件缓存,缓存依赖
- git_学习笔记
- 用户浏览器禁用了cookie,SessionID如何传递
- RPC和WebService等相关远程通信概念理解
- SQLserver中用convert函数转换日期格式
- 树的直径总结【模板】
- Linux等待队列wait_queue_head_t和wait_queue_t
- qt编译错误 _main 中被引用
- Linux内核配置(转)
- 类型“System.Net.Http.HttpResponseMessage”在未被引用的程序集中定
- 显示转换static_cast、const_cast、reinterpret_cast、dynamic_cast详解
- C语言之文件操作05——矩阵(数据)的读取方法
- LVS快速搭建教程
- iOS安全系列之一:HTTPS