题目来源:
http://acm.fzu.edu.cn/problem.php?pid=2160
代码如下:
using namespace std ; typedef long long LL ; const int Max_N = 100005; struct Point{ LL x,y; }; Point data[Max_N]; int ans[Max_N], Next[Max_N];// Next[i]表示i节点的下一个可连接节点 int n; // p0p1 X p0p2 左转为>0 返回0表示 不可通过 int judge(Point p1, Point p2, Point p0){ LL c= (p1.x - p0.x)*(p2.y-p0.y) - (p1.y - p0.y)*(p2.x-p0.x); if(c>=0) return 0; else return 1; } void solve(){ ans[n]=0; ans[n-1]=1; Next[n-1]=n; int u; for(int i=n-2; i>=1; i--){ u=i+1; while(u!=n && judge( data[Next[u]], data[u],data[i] )) u=Next[u]; Next[i]=u; ans[i] = ans[u] +1; } } int main(){ int t,k=1; scanf("%d",&t); while(t--){ // cin>>n; scanf("%d",&n); for(int i=1; i<= n; i++) scanf("%I64d%I64d",&data[i].x, &data[i].y); solve(); printf("Case#%d:",k++); for(int i=1; i<=n; i++) printf(" %d",ans[i]); printf(" "); } return 0; }