题意: 有一只右眼坏掉的外星虫子,只能左拐(逆时针转向),平面上有一些食物,设y坐标最小的食物的y坐标为y0,则虫子从(0,y0)出发,行走路径不能相交,也不能沿路返回,问最多吃掉多少食物。
解法: 不断求凸包
知识点: 凸包的求法(我用了Graham)
代码:
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#define N 200020
#define eps 1e-7
#define sosu double
using namespace std;
inline int read()
{int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;
}
int cas,n,m;
struct node{int id;sosu x,y;}p[N],o;
sosu cross(node s,node a,node b)
{sosu x1=a.x-s.x,x2=b.x-s.x;sosu y1=a.y-s.y,y2=b.y-s.y;return x1*y2-x2*y1;
}
sosu dis(node a,node b)
{return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}
bool operator < (node a,node b)
{sosu tmp=cross(o,a,b);if(fabs(tmp)>eps)return tmp>eps;else return dis(a,o)-dis(b,o)<-eps;
}
node st[N];int top,len,ans[N];bool vis[N];
int main()
{cas=read();while(cas--){memset(vis,0,sizeof(vis));top=0;memset(ans,0,sizeof(ans));len=0;memset(st,0,sizeof(st));n=read();o.x=0,o.y=1e9;for(int i=1;i<=n;i++){p[i].id=read(),scanf("%lf%lf",&p[i].x,&p[i].y);if(o.y>p[i].y)o=p[i];}ans[++len]=o.id,vis[o.id]=1;while(n>1){int nn=0;sort(p+1,p+1+n);st[1]=p[1],st[2]=p[2],top=2;for(int i=3;i<=n;i++){while(top>1&&cross(st[top-1],st[top],p[i])<-eps)top--;st[++top]=p[i];}for(int i=2;i<=top;i++)ans[++len]=st[i].id,vis[st[i].id]=1;for(int i=2;i<=n;i++)if(!vis[p[i].id])p[++nn]=p[i];p[++nn]=st[top];o=st[top];n=nn;}printf("%d ",len);for(int i=1;i<=len;i++)printf("%d ",ans[i]);puts("");}
}