2018多校赛第一场1003 Triangle Partition(排序水题)

题意

给定3N个点,保证任意三点不在一条直线上。要求输出N行,每行三个点,代表一个三角形。N个三角形不能相交。

解题

将3N个点按照x坐标升序排列。每三个组成一个三角形即可。

AC代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn=3e3+7;
struct node
{
    int x,y,num;
    node(int x,int y,int num):x(x),y(y),num(num){}
    node(){}
    bool operator<(const node &o)const{
        return x<o.x || x==o.x&&y<o.y;
    }
}a[maxn];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<3*n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            a[i]=node(x,y,i+1);
        }
        sort(a,a+3*n);
        int j=0;
        for(int i=0;i<n;i++)
        {
            printf("%d %d %d\n",a[j].num,a[j+1].num,a[j+2].num);
            j+=3;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37685156/article/details/81173526