题目描述:http://poj.org/problem?id=1696
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
const int MAXN=50;
using namespace std;
struct point
{
int x,y,index;
} p[MAXN+10];
point tmp;
int test;
int Xmul(point sp, point ep, point op)
{
return (sp.x - op.x) * (ep.y - op.y)-(ep.x - op.x) * (sp.y - op.y);
}
int len (const point & a,const point &b)
{
return (b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y);
}
bool cmp (const point & a,const point &b) //这个比较函数很高明啊!
{
if(Xmul(a,b,tmp)>0)
{
return true;
}
if(Xmul(a,b,tmp)==0)
{
if(len(a,tmp)<len(b,tmp))return true;
}
return false;
}
int main()
{
freopen("in.txt","r",stdin);
int n_case;
scanf("%d",&n_case);
while(n_case--)
{
scanf("%d",&test);
for(int i=0; i<test; i++)
{
scanf("%d%d%d",&p[i].index,&p[i].x,&p[i].y);
}
tmp=p[0];
int idx=0;
for(int i=1; i<test; i++)
{
if (p[i].y < tmp.y || (p[i].y == tmp.y && p[i].x < tmp.x))
{
tmp=p[i];
idx=i;
}
}
if(idx!=0) swap(p[0],p[idx]);
for(int i=1; i<test; i++)
{
tmp=p[i-1];
sort(p+i,p+test,cmp); //这里刚开始写错了,悲剧了,之前一直理解错误了,还是思维不完善;
}
printf("%d",test);
for(int i=0; i<test; i++)
{
printf(" %d",p[i].index);
}
printf("\n");
}
return 0;
}