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

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

时间: 2024-11-05 13:36:18

G++和C++ && POJ 1113 Wall的相关文章

poj 1113 Wall (凸包模板题)

Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32808   Accepted: 11137 Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he w

poj 1113 Wall 凸包的应用

题目链接:poj 1113   单调链凸包小结 题解:本题用到的依然是凸包来求,最短的周长,只是多加了一个圆的长度而已,套用模板,就能搞定: AC代码: 1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cmath> 5 using namespace std; 6 int m,n; 7 struct p 8 { 9 double x,y; 10 friend i

POJ 1113 Wall (凸包)

题目地址:POJ 1113 先求出凸包的周长,然后剩下的弧合起来一定是个半径为l的圆,然后再加上以l为半径的圆的周长即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <

POJ 1113 Wall 凸包 裸

LINK 题意:给出一个简单几何,问与其边距离长为L的几何图形的周长. 思路:求一个几何图形的最小外接几何,就是求凸包,距离为L相当于再多增加上一个圆的周长(因为只有四个角).看了黑书使用graham算法极角序求凸包会有点小问题,最好用水平序比较好.或者用Melkman算法 /** @Date : 2017-07-13 14:17:05 * @FileName: POJ 1113 极角序求凸包 基础凸包.cpp * @Platform: Windows * @Author : Lweleth (

poj 1113 Wall(标准的凸包果题)

题目链接:http://poj.org/problem?id=1113 Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he would not listen to his Architect's proposals to build

●POJ 1113 Wall

题链: http://poj.org/problem?id=1113 题解: 计算几何,凸包 题意:修一圈围墙把给出的点包围起来,且被包围的点距离围墙的距离不能小于L,求围墙最短为多少. 答案其实就是等于N个点的凸包的周长+半径为L的圆的周长. 代码: #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define

POJ - 1113 Wall (凸包)

http://poj.org/problem?id=1113 题意 求能包围城堡的最小周长,其中必须与城堡每个点相隔L. 分析 答案是凸包周长加上一个圆周长,即包围凸包的一个圆角多边形. 但那些圆角加起来为什么恰好是一个圆呢?每个圆角是以凸包对应的顶点为圆心,给定的L为半径,与相邻两条边的切点之间的一段圆弧. 每个圆弧的两条半径夹角与对应的凸包的内角互补.假设凸包有n条边,则所有圆弧角之和为180°*n-180°*(n-2)=360°.(凸边形内角和为(n-2)*180) 故,围墙周长为=n条平

简单几何(凸包) POJ 1113 Wall

题目传送门 题意:求最短路线,使得线上任意一点离城堡至少L距离 分析:先求凸包,答案 = 凸包的长度 + 以L为半径的圆的周长 /************************************************ * Author :Running_Time * Created Time :2015/10/25 11:00:48 * File Name :POJ_1113.cpp ************************************************/ #

POJ 1113 Wall 卷包裹法求凸包

Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31199   Accepted: 10521 Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he w