poj2187 Beauty Contest

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

Hint

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

 

题意:找两个距离最远的点,求它们的平方和

思路:凸包+旋转卡壳找最远距离

感想:又wa一晚上,本地样例都过了,提交就是wa。甚至在poj上还提交了五六发,全都wa...直到现在还找不出毛病来,真让人头大嗷。先开个坑,明天搞明白来填。

时隔好几天我来辽,人类的本质就是鸽子。还是换个模板吧,这是ac的代码,参照大佬队友的。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const double pi = acos(-1.0);
const int maxn = 1e5 + 10;
const double eps = 1e-6;
struct point{
	double x, y;
	point(){	
	}
	point(double _x, double _y)	{
		x = _x, y = _y;
	}	
}lst[maxn],p[maxn];
int stk[maxn], top;
int n, m;
point operator - (point a, point b){
	return point(a.x - b.x, a.y - b.y);
}
int sgn(double x) {
	if(fabs(x) < eps)
		return 0;
	else if(x < 0)
		return -1;
	else	return 1;
}
double cross(point a, point b) {//叉积 
	return a.x * b.y - a.y * b.x;
}

double Dis(point a, point b) { //距离 
	return pow(b.x - a.x, 2) + pow(b.y - a.y, 2);
}
bool cmp(point a, point b) {
	int t = sgn(cross(a - lst[0], b - lst[0]));
	if(t > 0)	
		return true;
	if(t == 0 && Dis(lst[0], a) < Dis(lst[0], b))
		return true;
	return false;
}
void graham() {
	int k = 0;
	point cur;
	cur.x = lst[0].x;
	cur.y = lst[0].y;
	for(int i = 1; i < n; i++) {  //找凸包的初始点 
		if(cur.y > lst[i].y || ((cur.y == lst[i].y) && (cur.x > lst[i].x))) {
			cur.x = lst[i].x;
			cur.y = lst[i].y;
			k = i;
		}
	}
	swap(lst[k], lst[0]);
	sort(lst + 1, lst + n, cmp);
	if(n == 1){
		top = 1;	
		stk[0] = 0;
		return;
	}
	if(n == 2) {
		top = 2;	
		stk[0] = 0;	
		stk[1] = 1;
		return;
	}
	stk[0] = 0;	
	stk[1] = 1;
	top = 2;
	for(int i = 2; i < n; i++) {
		while(top > 1 && cross(lst[stk[top - 1]] - lst[stk[top - 2]], lst[i] - lst[stk[top - 2]]) <= 0)
			top--;
		//叉积判断点在左侧不考虑 右侧加入 
		stk[top] = i;
		top++;
	}
	return;
}

void getmax() {
	int j = 2;
	double ans = 0;
	for(int i = 0; i <= top; i++) {
		while(fabs(cross(p[j] - p[i], p[i + 1] - p[i])) < fabs(cross(p[j + 1] - p[i], p[i + 1] - p[i])))
			j = (j + 1) % top;
		ans = max(ans, max(Dis(p[j], p[i + 1]), Dis(p[j], p[i])));
	}
	printf("%.0f", ans);
}

int main() {
	scanf("%d", &n);
	for(int i = 0; i < n; i++)
	{
		scanf("%lf%lf", &lst[i].x, &lst[i].y);
	}
	graham();
	stk[top] = 0;
	for(int i = 0; i <= top; i++)
	p[i] = lst[stk[i]];
	getmax();
	return 0;
}

这是我wa的代码,凸包算的也没问题,自己举得几个样例全过了,感觉哪里也没问题,可它就是不过...把错误代码也贴在这里了,如果有人知道错哪里了,欢迎留言  /呲牙

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxn 50010
using namespace std;
int n,k,top;
double ans=0;
struct point {
	double x;
	double y;
}num[maxn],st[maxn];
double dis(point p1,point p2) { //p1 p2的距离 
    return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
}
int cross(point p1,point p2,point p3) {//叉积 CA ×CB
    return (p1.x-p3.x)*(p2.y-p3.y)-(p2.x-p3.x)*(p1.y-p3.y);
}
bool cmp(point p1,point p2) {//极角排序 
    if(cross(p1,p2,num[0])>0) 
        return true;
    if(cross(p1,p2,num[0])==0&&dis(p1,num[0])<dis(p2,num[0]))//角度相同距离小的在前
        return true;
    return false;
}
void Graham() {//凸包
    top=2;
    sort(num+1,num+n,cmp);
    st[0]=num[0];
    st[1]=num[1];
    st[2]=num[2];
    for(int i=3; i<n; i++) {
        while(top>1&&cross(num[i],st[top],st[top-1])>=0)
            top--;
        st[++top]=num[i];
    }
    st[++top]=st[0]; //构造成一个凸包 
}

void qiake() {
    int q;
    for(int i=0; i<top; i++) {
        q=1;
        for(int p=i+1; p<top; p++) {//需要考虑到不一定是以p,p+1为底的三角形,
            while(cross(st[i],st[q+1],st[p])>cross(st[i],st[q],st[p])) //叉乘算面积 
                q=(q+1)%top;//不能超过凸包点的和 
            ans=max(ans,max(dis(st[p],st[i]),max(dis(st[p],st[q]),dis(st[i],st[q])) ) );
        }
    }
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin >> n;
	k=0;
	for(int i=0; i<n; i++) {
		cin >> num[i].x >> num[i].y;
		if(num[i].x<num[k].x || (num[i].x==num[k].x&&num[i].y<num[k].y) ) {
			k=i;
		}
	}
	swap(num[0],num[k]);
	if(n==2) {
		ans +=dis(num[0],num[1]);
	}
	else {
		Graham();
		qiake();	
	}
	cout<<ans<<endl;
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值