Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:数据不够强,用排序过的但好像发现有bugIn Reply To:数据不够强,用排序过的但好像发现有bug Posted by:zzqzzq at 2011-02-18 03:17:33 我的想法 0. 如果是随意给定的凸包点集 那么要先找到一个x坐标最小的点作为0点(这样可以保证没有第二象限的点) 1. 排序的时候如果叉积为0 那么先比较象限 象限相同再比较距离 2. 排序后将末尾共线的点次序翻转 代码及数据 /********************************* 极角排序(凸包,int) **********************************/ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <queue> #include <set> #include <map> #define inf 0x3f3f3f3f #define Inf 0x3FFFFFFFFFFFFFFFLL #define maxn 1005 using namespace std; struct Point{ int x, y; }p[maxn]; inline int cross(Point a, Point b){ return a.x * b.y - a.y * b.x; } bool cmp(Point a, Point b){ int t = cross(a, b); if(t == 0){ if(a.x * b.x < 0 || a.y * b.y < 0){ return a.y < b.y || a.y == b.y && a.x < b.x; } else{ return abs(a.x) < abs(b.x) || abs(a.y) < abs(b.y); } } else return t > 0; } void polar_sort(int n){ int mx = 0, x0, y0; for(int i=0;i<n;i++){ if(p[i].x < p[mx].x) mx = i; } swap(p[0], p[mx]); x0 = p[0].x, y0 = p[0].y; for(int i=0;i<n;i++){ p[i].x -= x0; p[i].y -= y0; } sort(p + 1, p + n, cmp); for(int i=n-1;i>=0;i--){ if(cross(p[i], p[i-1]) != 0){ reverse(p + i, p + n); break; } } for(int i=0;i<n;i++){ p[i].x += x0; p[i].y += y0; } } int main(){ int n; while(~scanf("%d", &n)){ int mx = 0, x0, y0; for(int i=0;i<n;i++){ scanf("%d%d", &p[i].x, &p[i].y); } polar_sort(n); for(int i=0;i<n;i++){ printf("%d %d\n", p[i].x, p[i].y); } } return 0; } /* 测试数据: 6 0 0 1 2 3 4 2 0 2 4 5 0 6 0 0 -3 0 -2 -2 -2 2 -1 1 -1 -1 6 0 0 2 2 -1 -1 -2 -2 2 -2 1 1 6 0 0 1 0 0 -2 0 1 0 2 0 -1 */ Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator