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;
}

希望大家明白啊!!!

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