G++和C++ && POJ 1113 Wall

时间:2014-04-27 21:44:59   收藏:0   阅读:346

PS: 次题目虽然叙述点的个数大于等于3但是并不保证凸包是否存在,所以还要判断一下。经常刷题的孩纸可能会遇到用C++ 可用AC的题目用G++ 却 Wrong Answer. 思考过为什么吗? 对于double 类型用%lf 输入用%lf输出是window 环境下VC的标准但不是真正的标准,对于double 类型 真正的标准是用%lf输入,用%f输出。所以把%.0lf改为%.0f 在G++环境下面就可用轻松AC了。

还有%lld 和 %I64d, 同时也学习一下控制精度的技巧,比如 printf("%d\n", (int)(ans+0.5)). 设置双精度下的毕竟函数等。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cmath>

using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int maxn  = 1010;

struct point {
	double x, y;
	point(double x=0, double y=0):x(x),y(y) {}
};

int sign(double x) {
	if(fabs(x)<eps) return 0;
	return x > 0 ? 1 : -1;
}
point operator - (point A, point B) {
	return point(A.x-B.x, A.y-B.y);
}
double Cross(point A, point B) {
	return A.x*B.y - A.y*B.x;
}
double mul(point P, point B, point C) {
	return Cross(B-P, C-P);
}
double dis2(point A, point B) {
	return (A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y);
}
double dis(point A, point B){
	double t1 = (A.x-B.x)*(A.x-B.x);
	double t2 = (A.y-B.y)*(A.y-B.y);
	return sqrt(t1+t2);
}
int n, l;
point p[maxn], q[maxn];
point s; // original.

bool cmp(point A, point B) {
	if(sign(mul(s, A, B))>0) return true;
	else if(sign(mul(s, A, B))==0 && dis2(s,A) < dis2(s, B))
		return true;
	else return false;
}
int convex_hull(point *a, int n, point *b) {
	for(int i = 1; i < n; i++) {
		if(sign(a[i].x-a[0].x)<0 || (sign(a[i].x-a[0].x)==0 && sign(a[i].y-a[0].y)<0)) {
			swap(a[0],a[i]);
		}
	}
	s = a[0];
	sort(a, a+n, cmp);
	int newn = 2;
	b[0]=a[0], b[1]=a[1];
	for(int i = 2; i < n; i++) {
		while(newn>1 && sign(mul(b[newn-1], b[newn-2], a[i]))>=0)
			--newn;
		b[newn++] = a[i];
	}
	return newn;
}
int main() {
	scanf("%d%d", &n, &l);
	for(int i = 0; i < n; i++) {
		scanf("%lf%lf", &p[i].x, &p[i].y);
	}
	int len = convex_hull(p, n, q);
	if(len < 3) {
        printf("0\n");
        return 0;
    }
	q[len] = q[0];
	double ans = 0;
	for(int i = 0; i < len; i++) {
		ans += dis(q[i], q[i+1]);
	}
	ans += 2*pi*l;
    printf("%.0f\n", ans); 
#ifdef M
printf("%.0f\n", ans);  // G++ AC,C++ AC。
printf("%d\n",(int)(ans+0.5)); G++ AC.
printf("%.0lf\n", ans); C++ AC, G++ WA.
#endif
	return 0;
}

G++和C++ && POJ 1113 Wall,码迷,mamicode.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!