結果

問題 No.3154 convex polygon judge
ユーザー GOTKAKO
提出日時 2025-05-20 21:33:48
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 2,656 bytes
コンパイル時間 1,769 ms
コンパイル使用メモリ 204,812 KB
実行使用メモリ 13,372 KB
最終ジャッジ日時 2025-05-20 21:33:52
合計ジャッジ時間 3,463 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 44
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<typename T>
struct Point{
    public:
    T x,y;
    Point() : x(0),y(0) {}
    Point(T a,T b) : x(a),y(b) {}
    Point &operator=(const Point &b) = default;
    Point operator+(const Point &b){return Point(x+b.x,y+b.y);}
    Point operator-(const Point &b){return Point(x-b.x,y-b.y);}
    Point operator+=(const Point &b){return *this=*this+b;}
    Point operator-=(const Point &b){return *this=*this-b;}
    bool operator==(const Point &b){return x==b.x && y==b.y;}
    bool operator!=(const Point &b){return x!=b.x || y!=b.y;}
    bool operator<(const Point &b){
        if(x == b.x) return y < b.y;
        else return x < b.x; 
    }
    bool operator<=(const Point &b){return (*this) < b || (*this) == b;}
    bool operator>(const Point &b){return !((*this) <= b);}
    bool operator>=(const Point &b){return !((*this) < b);}

    friend T inner(const Point a,const Point b){return a.x*b.x+a.y*b.y;}
    friend T cross(const Point a,const Point b){return a.x*b.y-a.y*b.x;}
    friend T twodist(const Point a,const Point b){return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}//2乗で返す.
};
template<typename T>
istream &operator>>(istream &is,Point<T> &a){
    is >> a.x >> a.y;
    return is;
}
template<typename T>
ostream &operator<<(ostream &os,Point<T> &a){
    os << a.x << " " << a.y;
    return os;
}
template<typename T>
vector<Point<T>> Convexhull(vector<Point<T>> &P){
    //(x,y)が最小の点から時計回りに頂点を返す.
    //(xi,yi)と(xi+1,yi+1)は辺になる |P|=1の時注意.
    sort(P.begin(),P.end());
    P.erase(unique(P.begin(),P.end()),P.end());
    if(P.size() == 0) return {};
    if(P.size() == 1) return {P.at(0),P.at(0)};
    vector<Point<T>> A,B;
    for(auto &p1 : P){
        while(A.size() >= 2){
            auto &p2 = A.at(A.size()-1);
            auto &p3 = A.at(A.size()-2);
            T check = cross(p2-p1,p3-p2);
            if(check <= 0) A.pop_back();
            else break;
        }
        A.push_back(p1);
    }
    for(auto &p1 : P){
        while(B.size() >= 2){
            auto &p2 = B.at(B.size()-1);
            auto &p3 = B.at(B.size()-2);
            T check = cross(p2-p1,p3-p2);
            if(check >= 0) B.pop_back();
            else break;
        }
        B.push_back(p1);
    }
    for(int i=B.size()-2; i>=0; i--) A.push_back(B.at(i));
    return A;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int N; cin >> N;
    vector<Point<long long>> P(N);
    for(auto &p : P) cin >> p;
    auto C = Convexhull(P);
    if(C.size() == N+1) cout << "Yes\n";
    else cout << "No\n"; 

}   
0