hdu 4946 Area of Mushroom(凸包)

时间:2021-06-20 22:17:38

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4946

Area of Mushroom

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1896    Accepted Submission(s): 452

Problem Description
Teacher Mai has a kingdom with the infinite area.

He has n students guarding the kingdom.

The i-th student stands at the position (xi,yi), and his walking speed is vi.

If a point can be reached by a student, and the time this student walking to this point is strictly less than other students, this point is in the charge of this student.

For every student, Teacher Mai wants to know if the area in the charge of him is infinite.

Input
There are multiple test cases, terminated by a line "0".

For each test case, the first line contains one integer n(1<=n<=500).

In following n lines, each line contains three integers xi,yi,vi(0<=|xi|,|yi|,vi<=10^4).

Output
For each case, output "Case #k: s", where k is the case number counting from 1, and s is a string consisting of n character. If the area in the charge of the i-th student isn't infinite, the i-th character is "0", else it's "1".
Sample Input
3
0 0 3
1 1 2
2 2 1
Sample Output
Case #1: 100
Source

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

直接引用这里了:http://blog.csdn.net/hcbbt/article/details/38582243

题意: 
给定n个人,每个人的坐标和移动速度v,若对于某个点,只有 x 能最先到达(即没有人能比x先到这个点或者同时到这个点),则这个点称作被x占有,若有人能占有无穷大的面积 则输出1 ,否则输出0。

分析: 
到最后只有速度最大的点才有可能获得无穷大的面积。所以只要考虑速度最大的点。 
很明显,只有这些点的凸包边上的点才能获得无穷大的面积。 
所以求凸包边上的点就行了。

有几个要注意的坑就是: 
1. 如果有点(x,y,v)都相同,那这个点是无法占领无限大的,但是不能不考虑这个点进行凸包,因为这个点会对其他点产生影响。 
2. 如果最大速度为0,那么每个点都不会动,所以就不用进行凸包了。

====================================================

多校的题目质量确实高,就这题来说,设了几个坑,最大速度为零的情况,以及三点共线的情况,还有三点在边界上的情况

凸包有两种构建方法,一个是用极角排序,再循环一次

二是直接坐标排序,循环两次

但是第一种情况,对于三点在边界上的情况会少了最后一个点

这也是为什么网上的题解差不多都是第二种方法的原因,第一种方法,如果想用的话,必须先建凸包,

再判断是否在凸包的两点之间那条线上,在的话就保留,当然,会费些时间

----------------------------------------------------------------------------------

第二种方法建凸包:

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm> using namespace std;
#define MAXX 1110
#define eps 1e-8 typedef struct point
{
double x;
double y;
int v;
int id;
} point; bool dy(double x,double y)
{
return x>y+eps;
}
bool xy(double x,double y)
{
return x<y-eps;
}
bool dyd(double x,double y)
{
return x>y-eps;
}
bool xyd(double x,double y)
{
return x<y+eps;
}
bool dd(double x,double y)
{
return fabs(x-y)<eps;
} double crossProduct(point a,point b,point c)
{
return (a.x-c.x)*(b.y-c.y)>(b.x-c.x)*(a.y-c.y);
}
double dist(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} point p[MAXX];
point stk[MAXX];
int top; bool cmp(point a,point b)
{
if( dd(a.y,b.y))
return xy(a.x,b.x);
return xy(a.y,b.y);
} void Graham(int n)
{
top=;
sort(p,p+n,cmp);
if(n == )
{
top=-;
return ;
}
stk[]=p[];
if(n == )
{
top=;
return;
}
stk[]=p[];
if(n == )
{
top=;
return;
}
stk[]=p[];
for(int i=; i<n; i++)
{
while(top && crossProduct(p[i],stk[top],stk[top-]))
top--;
stk[++top]=p[i];
}
int len=top;
stk[++top]=p[n-];
for(int i=n-; i>=; i--)
{
while(top != len && crossProduct(p[i],stk[top],stk[top-]))
top--;
stk[++top]=p[i];
}
} int main()
{
int i,j,n,m;
int cas=; while(scanf("%d",&n)!=EOF && n)
{
point tmp[MAXX];
int mark[MAXX];
int maxx=;
memset(mark,,sizeof(mark)); for(i=; i<n ; i++)
{ scanf("%lf%lf%d",&tmp[i].x,&tmp[i].y,&tmp[i].v);
if(maxx < tmp[i].v)
{
maxx=tmp[i].v;
}
tmp[i].id=i;
}
if(maxx == )
{ printf("Case #%d: ",cas++);
for(i=; i<n; i++)
printf("%d",mark[i]);
printf("\n");
continue;
}
int id[MAXX];
memset(id,,sizeof(id));
for(i=; i<n; i++)
{ for(j=i+; j<n; j++)
{ if(tmp[i].x == tmp[j].x && tmp[i].y == tmp[j].y && tmp[i].v == tmp[j].v)
{ tmp[i].v=;
id[i]=id[j]=-;
}
}
}
int ss=;
for(i=; i<n; i++)
{ if(maxx == tmp[i].v)
{ p[ss++] = tmp[i];//can??
}
}
Graham(ss);
for(i=; i<=top; i++)
{ if(id[stk[i].id] != -)
{ id[stk[i].id] = ;
}
}
printf("Case #%d: ",cas++);
for(i=; i<n; i++)
printf("%d",id[i]>?:);
printf("\n");
}
return ;
} /*
5
0 0 6
3 3 6
1 1 6
0 3 6
3 0 6
Case #1: 11011
9
0 0 3
0 1 3
0 2 3
1 0 3
1 1 3
1 2 3
2 0 3
2 1 3
2 2 3
Case #2: 111101111
3
0 0 3
1 1 2
2 2 1
Case #3: 100
3
0 0 3
0 0 3
0 0 3
Case #4: 000
8
1 1 3
2 1 3
3 1 3
3 2 3
2 2 3
1 2 3
1 3 3
3 3 3
Case #5: 11110111
4
0 0 3
0 3 3
3 0 3
1 1 3
Case #6: 1110
6
0 0 1
-1 0 1
1 0 1
0 1 1
0 -1 1
0 -1 1
Case #7: 011100
*/

第一种方法建凸包但wa的:

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include<iostream>
#include <algorithm> using namespace std;
#define MAXX 1110
#define eps 1e-8 typedef struct point
{ double x;
double y;
int v;
int id;
} point; bool dy(double x,double y)
{
return x>y+eps;
}
bool xy(double x,double y)
{
return x<y-eps;
}
bool dyd(double x,double y)
{
return x>y-eps;
}
bool xyd(double x,double y)
{
return x<y+eps;
}
bool dd(double x,double y)
{
return fabs(x-y)<eps;
} double crossProduct(point a,point b,point c)
{ return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
}
double dist(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} point p[MAXX];
point stk[MAXX];
int top; bool cmp(point a,point b)
{ double len=crossProduct(p[],a,b);
if(dd(len,0.0))
{ return xy(dist(p[],a),dist(p[],b));
}
return xy(len ,0.0);
} int Graham(int n)
{ int tmp=;
if(n<) return ;
for(int i=; i<n; i++)
{ if(xy(p[i].x,p[tmp].x) || dd(p[i].x,p[tmp].x) && xy(p[i].y,p[tmp].y))
tmp=i;
}
swap(p[],p[tmp]);
sort(p,p+n,cmp);
top=;
stk[]=p[];
stk[]=p[];
for(int i=; i<n; i++)
{
while(top && xy(crossProduct(stk[top],stk[top-],p[i]),0.0))
top--;
stk[++top]=p[i];//会少一个点
}
/*int len=top;
stk[++top] = p [n-2];
for(int i=n-3; i>=0; i--)
{
while(top != len && xy(crossProduct(stk [top],stk[top-1],p[i]),0.0))
top--;
stk[++top] = p[i];
}*/ return top;
} int main()
{ int i,j,n,m;
int cas=; while(scanf("%d",&n)!=EOF && n)
{
point tmp[MAXX];
int mark[MAXX];
int maxx=;
memset(mark,,sizeof(mark));
for(i=; i<n ; i++)
{ scanf("%lf%lf%d",&tmp[i].x,&tmp[i].y,&tmp[i].v);
if(maxx < tmp[i].v)
{
maxx=tmp[i].v;
}
tmp[i].id=i;
}
if(maxx == )
{
printf("Case #%d: ",cas++);
for(i=; i<n; i++)
printf("%d",mark[i]);
printf("\n");
continue;
}
int id[MAXX];
memset(id,,sizeof(id));
for(i=; i<n; i++)
{ for(j=i+; j<n; j++)
{
if(tmp[i].x == tmp[j].x && tmp[i].y == tmp[j].y &&tmp[i].v == tmp[j].v)
{
tmp[i].v=;
id[i]=id[j]=-;
}
}
}
int ss=;
for(i=; i<n; i++)
{
if(maxx == tmp[i].v)
{
p[ss++]=tmp[i];//can??
}
}
int top=Graham(ss);
for(i=; i<=top; i++)
{
if(id[stk[i].id] != -)
{
id[stk[i].id] = ;
}
}
printf("Case #%d: ",cas++);
for(i=; i<n; i++)
printf("%d",id[i]>?:);
printf("\n");
}
return ;
} /*
5
0 0 6
3 3 6
1 1 6 0 3 6
3 0 6
Case #1: 11011
9
0 0 3
0 1 3
0 2 3
1 0 3
1 1 3
1 2 3
2 0 3
2 1 3
2 2 3
Case #2: 111101111
3
0 0 3
1 1 2
2 2 1
Case #3: 100
3
0 0 3
0 0 3
0 0 3
Case #4: 000
8
1 1 3
2 1 3
3 1 3
3 2 3
2 2 3
1 2 3
1 3 3
3 3 3
Case #5: 11110111
4
0 0 3
0 3 3
3 0 3
1 1 3
Case #6: 1110
6
0 0 1
-1 0 1
1 0 1
0 1 1
0 -1 1
0 -1 1
Case #7: 011100
*/

网上找的第一种方法AC的:(希望原创别喷我)

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include <iostream>
#define EPS 1e-8
#define eps 1e-8
using namespace std;
struct TPoint
{
double x,y;
int id,v;
}p[],s[],hull[],pp[];
double cross(TPoint a, TPoint b, TPoint c) {
return (b.x - a.x) * (c.y - a.y) - (c.x - a.x)*(b.y - a.y);
}
double dis(TPoint a,TPoint b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool graham_cmp(const TPoint &b, const TPoint &c) {
double tmp = cross(b, c, p[]);
if (tmp > EPS)
return true;
if (fabs(tmp) < EPS && (dis(b, p[]) < dis(c, p[])))
return true;
return false;
}
int graham_scan(TPoint hull[], int n) {
int top, i, k = ;
for (i = ; i < n; ++i)
if ((p[k].y - p[i].y > EPS)
|| (fabs(p[i].y - p[k].y) < EPS && p[k].x - p[i].x > EPS))
k = i;
swap(p[], p[k]);
sort(p + , p + n, graham_cmp);
hull[] = p[], hull[] = p[], hull[] = p[];
if (n < )
return n;
else
top = ;
for (i = ; i < n; ++i) {
while (top >= && cross(hull[top - ], hull[top - ], p[i]) < EPS)
--top;
hull[top++] = p[i];
}
return top;
}
bool bo[];
int ans[];
bool cmp(TPoint a,TPoint b)
{
return a.x<b.x-eps||(fabs(a.x-b.x)<eps&&a.y<b.y);
}
int main() {
int ri=,n;
while(scanf("%d",&n)&&n)
{
int maxn=;
for(int i=;i<n;++i)
ans[i]=;
for(int i=;i<n;++i)
{
scanf("%lf%lf%d",&s[i].x,&s[i].y,&s[i].v);
s[i].id=i;
maxn=std::max(maxn,s[i].v);
}
if(maxn==)
{
printf("Case #%d: ",++ri);
for(int i=;i<n;++i)
printf("");
puts("");
continue;
}
int tail=;
for(int i=;i<n;++i)
{
if(s[i].v==maxn&&maxn>)
{
pp[tail]=s[i];
p[tail++]=s[i];
}
}
sort(p,p+tail,cmp);
int kk=;
for(int i=;i<tail;++i)
if(i==tail-||fabs(p[i].x-p[i+].x)>eps||fabs(p[i].y-p[i+].y)>eps)
p[kk++]=p[i];
int h=graham_scan(hull,kk);
hull[h]=hull[];
for(int i=;i<tail;++i)
{
int flag=;
for(int j=;j<tail;++j)
if(i!=j&&fabs(pp[i].x-pp[j].x)<eps&&fabs(pp[i].y-pp[j].y)<eps)
{
flag=;
break;
}
if(flag)
{
ans[pp[i].id]=;
continue;
}
bool ok=false;
for(int j=;j<h;++j)
{
if(fabs(cross(pp[i],hull[j],hull[j+]))<eps)
ok=true;
}
if(ok)
ans[pp[i].id]=;
else
ans[pp[i].id]=;
}
printf("Case #%d: ",++ri);
for(int i=;i<n;++i)
printf("%d",ans[i]);
puts(""); }
}