HDU 6300(2018多校第一场C)(极角排序)

传送门

题面:

Triangle Partition

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 38    Accepted Submission(s): 20
Special Judge

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

Source

2018 Multi-University Training Contest 1

 

题目描述:

    给你3*n个点,规定这3*n个点不会三点共线。让你构造出n个不相交的三角形,并将这n个三角形的点按顺序输出。

题目分析:

    因为题目给了我们3*n个点,因此我们一定是可以构成n个三角形的。

    紧接着,题目要求我们使得所有三角形不能有相交,因此,我们可以考虑,取最左下的点p[0]作为基准点,进行一次极角排序,使得其他的结点都按照p[0]进行排序。这就使得之后我们能够取到的所有的点都是按照极角的顺序的,进而使得所形成的三角形不会相交。

代码:

#include <bits/stdc++.h>
#define maxn 4005
typedef long long ll;
using namespace std;
const double eps=1e-8;
int sgn(double x){
    if(fabs(x)<eps) return 0;
    if(x<0) return -1;
    else return 1;
}
struct Point{
    double x,y;
    int index;
    Point(){}
    Point(int _x,int _y){
        x=_x,y=_y;
    }
    Point operator -(const Point &b)const{
        return Point(x-b.x,y-b.y);
    }
    double operator *(const Point &b)const{
        return x*b.x+y*b.y;
    }
    double operator ^(const Point &b)const{
        return x*b.y-y*b.x;
    }
};
Point p[maxn];
int pos;
double dist(Point a,Point b){
    return sqrt((a-b)*(a-b));
}
bool cmp(Point a,Point b){
    double tmp=(a-p[pos])^(b-p[pos]);
    if(sgn(tmp)<0) return false;
    else if(sgn(tmp)>0) return true;
    else return dist(p[pos],a)<dist(p[pos],b);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        pos=0;
        int n;
        scanf("%d",&n);
        for(int i=0;i<3*n;i++){
            scanf("%lf%lf",&p[i].x,&p[i].y);
            p[i].index=i+1;
            if(p[0].y>p[i].y||(p[i].y==p[0].y&&p[0].x>p[i].y)){
                swap(p[i],p[0]);
            }
        }
        sort(p+1,p+3*n,cmp);
        int cnt=1;
        for(int i=0;i<3*n;i++){
            if(cnt==1) printf("%d",p[i].index);
            else printf(" %d",p[i].index);
            cnt++;
            if(cnt==4){
                printf("\n");
                cnt=1;
            }
        }
    }
    return 0;
}

深刻反思:

    比赛过程中的时候,我把竟然竟然竟然cmp函数居然写错了!!!!然后就是多达6发的wa以及大量精力的浪费QAQ。对不起队友信任啊QAQ。(深刻反思深刻反思QAQ)

猜你喜欢

转载自blog.csdn.net/weixin_39453270/article/details/81172539