Problem G. Interstellar Travel

思路:其实是求上凸包的所有点,注意去重

Claris出的题,第一眼就知道是求凸包上的点,但问题出在字典序最小的情况。不知道怎么贪心,还有对凸包的理解不够深刻。写这题起码要能理解求凸包每一步的过程,不是那种套套模板就可以过的题

#include<cstdio>
#include<vector>
#include<cmath>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#define PI acos(-1.0)
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=3e5+5;
const int MOD=1e9+7;
template <class T>
bool sf(T &ret){ //Faster Input
    char c; int sgn; T bit=0.1;
    if(c=getchar(),c==EOF) return 0;
    while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    if(c==' '||c=='\n'){ ret*=sgn; return 1; }
    while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
    ret*=sgn;
    return 1;
}
int sign(double x){
    return abs(x)<1e-7?0:x<0?-1:1;
}
struct Point{
    ll x,y,id;
    Point(ll x=0, ll y=0) : x(x), y(y) {}
    Point operator - (const Point &rhs) const{
        return Point(x-rhs.x,y-rhs.y);
    }
    bool operator == (const Point &rhs) const{
        return sign(x-rhs.x)==0&&sign(y-rhs.y)==0;
    }
    bool operator < (const Point &rhs)const{
        if(x==rhs.x && y==rhs.y)    return id<rhs.id;
        if(x==rhs.x)    return y<rhs.y;
        else    return x<rhs.x;
    }
}p[N];
Point ch[N];
typedef Point Vector;
ll cross(Vector A,Vector B){
    return A.x*B.y-A.y*B.x;
}
int n;
typedef vector<Point> Polygon;
Polygon convex_hull(Polygon p) {
    sort(p.begin(),p.end());  //排序
    int k=0;
    int n=p.size();
    Polygon Q(2*n);
    for (int i=0; i<n; ++i) {//突然地发现这是求下凸包
        if(k>0 && p[i].x==Q[k-1].x && p[i].y==Q[k-1].y)     continue;
        while (k > 1 ){
            if(cross(Q[k-1]-Q[k-2], p[i]-Q[k-2]) == 0){
//                cout << p[i].id <<"vs" << Q[k-1].id<<endl;
                if(p[i].id<Q[k-1].id)   k--;
                else    break;
            }
            else if(cross(Q[k-1]-Q[k-2], p[i]-Q[k-2]) >0 ) k--;
            else    break;
        }
        Q[k++] = p[i];
    }
    Q.resize(k);
    return Q;
}

int main(void){
    int T;
    cin >>T;
    while(T--){
        sf(n);
        Polygon t;
        for(int i=1;i<=n;i++){
            int x,y;
            scanf("%d%d",&x,&y);
            p[i].x=x,p[i].y=y,p[i].id=i;
            t.push_back(p[i]);
        }
        vector<Point> ans=convex_hull(t);
        printf("%d",ans[0].id);
        for(int i=1;i<(int)ans.size();i++)  printf(" %d",ans[i].id);
        printf("\n");
    }
    return 0;
}