Triangle Partition

Triangle Partition

Problem Description

Chiaki has 3n points p1,p2,…,p3n. It is guaranteed that no three points are collinear.
Chiaki would like to construct n disjoint triangles where each vertex comes from the 3n points.

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤1000) – the number of triangle to construct.
Each of the next 3n lines contains two integers xi and yi (−109≤xi,yi≤109).
It is guaranteed that the sum of all n does not exceed 10000.

Output

For each test case, output n lines contain three integers ai,bi,ci (1≤ai,bi,ci≤3n) each denoting the indices of points the i-th triangle use. If there are multiple solutions, you can output any of them.

Sample Input

1
1
1 2
2 3
3 5

Sample Output

1 2 3

题目概述

三角形分区

问题描述

Chiaki有3n个点p1 p2…p3n。保证没有三点共线。

Chiaki想要构造n个不连续三角形每个顶点来自3n个点。

输入

有多个测试用例。第一行输入包含一个整数T,表示测试用例的数量。为每个测试用例:

第一行包含一个整数n(1≤n≤1000)——三角形构造的数量。

每个下3 n行包含两个整数习近平和易建联(109−≤xi,易建联≤109)。

保证所有n的总和不超过10000。

输出

对于每个测试用例,输出n行包含三个整数ai,bi,ci(1≤ai,bi,ci≤3 n)每个表示点的第i个三角形的指标使用。如果有多个解,您可以输出其中任何一个。

样例输入

1

1

1 2

2 3

3 5

样例输出

1 2 3

思路

题中说明不可能有三点共线的情况 那么要想让三角形之间两两不相交不相连,就按合纵座标依次增大或减小排列,这要相邻的每三个点组成一个三角形,得到的绝对既不相交也不相连
如图所示 给出(1,1),(5,6),(2,6),(9,5),(7,2),(5,1)几个点
把他们排序后 (1,1),(2,6),(5,1),(5,6),(7,2),(9,5)
或者为 (9,5),(7,2),(5,6),(5,1),(2,6),(1,1)
相邻三个点相连
即(1,1),(2,6),(5,1)相连 。(5,6),(7,2),(9,5)相连。构成两个三角形,第一个三角形用到的点分别是1,3,6号点。第二个三角形用到的点分别是2,5,4(按输入点的顺序编号,第一个输入的点是1号,第二个输入的点为2号…..)
故输出为
1 3 6
2 5 4
这里写图片描述

代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

struct  shu
{
    int z;       //用来编辑编号(第几个输入的点)
    int x,y;     //存储座标
}s[30005];
bool cmp(shu a,shu b)
{
    if(a.x==b.x)   //如果横座标相同 按纵座标从小到大排列
        return a.y<b.y;   
    return a.x<b.x; //如果横座标不同 按横座标从小到大排列
}
int main()
{
    int i,j,n,T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(i=0;i<n*3;i++)
        {
            cin >> s[i].x >> s[i].y;
            s[i].z=i+1;
        }
        sort(s,s+3*n,cmp);  //排列点
        for(i=0;i<n;i++)    //将这n个三角形输出,每3个点为一组
        {
            printf("%d %d %d\n",s[i*3].z,s[i*3+1].z,s[i*3+2].z); 
        }
    }
    return 0;
}

希望大家明白啊!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章