poj 3787 Convex Hull of Lattice Points 求凸包

题意:

裸的凸包。

分析:

graham模板直接上。

代码:

//poj 3787
//sep9
#include <iostream>
#include <algorithm>
using namespace std;
const int maxN=64;

struct P
{
	int x,y;
}pnt[maxN],cnt[maxN];
int n;

int cmp(P a,P b)
{
	if(a.y!=b.y)
		return a.y<b.y;
	return a.x<b.x;
}

int cross(P a,P b,P c)
{
	int x1=b.x-a.x;
	int y1=b.y-a.y;
	int x2=c.x-a.x;
	int y2=c.y-a.y;
	return x1*y2-x2*y1;
}
int graham()
{
	sort(pnt,pnt+n,cmp);
	int i,pos=1;
	cnt[0]=pnt[0];
	cnt[1]=pnt[1];
	for(i=2;i<n;++i){
		while(pos>0&&cross(cnt[pos-1],cnt[pos],pnt[i])<=0) --pos;
		cnt[++pos]=pnt[i];
	}
	int bak=pos;
	for(i=n-2;i>=0;--i){
		while(pos>bak&&cross(cnt[pos-1],cnt[pos],pnt[i])<=0) --pos;
		cnt[++pos]=pnt[i];
	}
	return pos;
}

int main()
{
	int cases,case_num;
	scanf("%d",&cases);
	while(cases--){
		scanf("%d%d",&case_num,&n);
		int pos;
		for(int i=0;i<n;++i)
			scanf("%d%d",&pnt[i].x,&pnt[i].y);
		printf("%d %d\n",case_num,pos=graham());
		int index=0;
		for(int i=1;i<pos;++i)
			if(cnt[i].y>cnt[index].y)
				index=i;
			else if(cnt[i].y==cnt[index].y&&cnt[i].x<cnt[index].x)
				index=i;
		int mod=pos;
		while(pos--){
			printf("%d %d\n",cnt[index].x,cnt[index].y);
			--index;
			index+=mod;
			index%=mod;
		}
	}
	return 0;
} 
时间: 2024-10-11 05:41:30

poj 3787 Convex Hull of Lattice Points 求凸包的相关文章

【HDOJ】3285 Convex Hull of Lattice Points

凸包模板题目. 1 /* 3285 */ 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <algorithm> 7 using namespace std; 8 9 #define MAXN 55 10 11 typedef struct { 12 int x, y; 13 } Point_t; 14 15

POJ 3090 Visible Lattice Points 法雷级数

题目来源:POJ 3090 Visible Lattice Points 题意:哪些点可以看到 思路: F1: 0/1 1/1 F2: 0/1 1/2 1/1 F3: 0/1 1/3 1/2 2/3 1/1 F4: 0/1 1/4 1/3 1/2 2/3 3/4 1/1 F5: 0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1 F6:0/1 1/6 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 5/6 1/1 法雷级数 可以看到的那

poj 3060 Visible Lattice Points

http://poj.org/problem?id=3090 Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6153   Accepted: 3662 Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other

数论 - 欧拉函数的运用 --- poj 3090 : Visible Lattice Points

Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5636   Accepted: 3317 Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible fr

POJ 3090 Visible Lattice Points 欧拉函数

链接:http://poj.org/problem?id=3090 题意:在坐标系中,从横纵坐标 0 ≤ x, y ≤ N中的点中选择点,并且这些点与(0,0)的连点不经过其他的点. 思路:显而易见,x与y只有互质的情况下才会发生(0,0)与(x,y)交点不经过其他的点的情况,对于x,y等于N时,可以选择的点均为小于等于N并且与N互质的数,共Euler(N)个,并且不重叠.所以可以得到递推公式aa[i]=aa[i]+2*Euler(N). 代码: #include <iostream> #in

POJ 3090 Visible Lattice Points

Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible from the origin if the line from (0, 0) to (x, y) does not pass through any other lattice point. For example

POJ 3090 Visible Lattice Points 布雷级数

Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5779   Accepted: 3409 Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible fr

poj 3090 Visible Lattice Points 法雷级数||打表

由于图像关于对角线对称,所以我们只看下三角区域.将x轴看做分母,被圈的点看成分子 依次是{1/2},{1/3,1/2},{1/4,3/4},{1/5,2/5,3/5,4/5} 写成前缀和的形式就是 {1/2},{1/2,1/3,2/3},{1/2,1/3,1/3,1/4,3/4},{1/2,1/3,1/3,1/4,3/4,1/5,2/5,3/5,4/5} 发现,这就是一个法雷级数,即第k项增加的数就是phi[k].最后的答案*2+(0,1)+(1,0),(1,1)三个点就好了 #include

【POJ】3090 Visible Lattice Points

Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7705   Accepted: 4707 Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible fr