1. 程式人生 > >HDU6300-Triangle Partition

HDU6300-Triangle Partition

HDU6300-Triangle Partition

題目:
Chiaki has
3n
3n
points
p
1
,
p
2
,…,
p
3n
p1,p2,…,p3n
. It is guaranteed that no three points are collinear.
Chiaki would like to construct
n
n
disjoint triangles where each vertex comes from the
3n
3n
points.
Input
There are multiple test cases. The first line of input contains an integer
T
T
, indicating the number of test cases. For each test case:
The first line contains an integer
n
n
(
1≤n≤1000
1≤n≤1000
) – the number of triangle to construct.
Each of the next
3n
3n
lines contains two integers
x
i
xi
and
y
i
yi
(

10
9

x
i
,
y
i

10
9
−109≤xi,yi≤109
).
It is guaranteed that the sum of all
n
n
does not exceed
10000
10000
.
Output
For each test case, output
n
n
lines contain three integers
a
i
,
b
i
,
c
i
ai,bi,ci
(
1≤
a
i
,
b
i
,
c
i
≤3n
1≤ai,bi,ci≤3n
) each denoting the indices of points the
i
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

程式碼如下:

#include<bits/stdc++.h>
using namespace std;
#define MAX 8000

struct point
{
    int x;
    int y;
    int num;
};

int main()
{
    int T,n,mina;
    struct point a[MAX];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i = 0;i < 3 * n;i++)
        {
            cin >> a[i].x >> a[i].y;
            a[i].num = i + 1;
        }

        for(int i = 0;i < 3 * n;i++)
        {
            mina = i;
            for(int j = i + 1;j < 3 * n;j++)
            {
                if(a[j].x < a[mina].x) mina = j;
            }
            swap(a[mina],a[i]);
        }

        for(int i = 0;i < 3 * n;)
        {
            cout << a[i].num << " "<< a[i + 1].num << " " << a[i + 2].num << endl;
            i += 3;
        }
    }
    return 0;
}

題目要求給出3n個點,並且每三個點都不共線。每次要求輸出n組三角形的點的索引(1組輸出三個點的索引)。這道題的思路是先建立一個結構體,每個結構體都代表一個點,結構體中有點的橫座標,縱座標,以及點的索引。因為題目要求輸出的三角形之間不能重合,所以在這裡我將這些點根據橫座標的從小到大進行排序。那麼,每次輸出的三角形都不會重合。在這裡我使用的選擇排序(當然了,後來發現sort的耗時更少,所以也可以用sort進行排序),隨後進行輸出就可以了。