結果

問題 No.3154 convex polygon judge
ユーザー Cafe1942
提出日時 2025-05-10 14:05:51
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 2,513 bytes
コンパイル時間 1,027 ms
コンパイル使用メモリ 86,064 KB
実行使用メモリ 13,816 KB
最終ジャッジ日時 2025-05-10 17:03:40
合計ジャッジ時間 3,344 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 44
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    cin >> N;
    pair<long long, long long>P[200002];
    for (int i = 0;i < N;i++) {
        cin >> P[i].first >> P[i].second;
    }
    sort(P, P + N);//座標を辞書順ソート
    stack<pair<long long, long long>>top, bot;
    top.push(P[0]);//上回りで凸包をつくる。
    top.push(P[1]);
    for (int i = 2;i < N;i++) {
        while (1) {
            pair<long long, long long> top0 = top.top();
            top.pop();
            if ((top0.first - top.top().first) * (P[i].second - top0.second) - (top0.second - top.top().second) * (P[i].first - top0.first) < 0) {//ベクトルの外積で内角が180度以上になるか判定
                top.push(top0);//内角が180度未満の場合、採用。
                top.push(P[i]);
                break;
            }
            else if (top.size() == 1) {
                top.push(P[i]);
                break;
            }
        }
    }
    bot.push(P[0]);//下回りで凸包をつくる。
    bot.push(P[1]);
    for (int i = 2;i < N;i++) {
        while (1) {
            pair<long long, long long> top0 = bot.top();
            bot.pop();
            if ((top0.first - bot.top().first) * (P[i].second - top0.second) - (top0.second - bot.top().second) * (P[i].first - top0.first) > 0) {//ベクトルの外積で内角が180度以上になるか判定
                bot.push(top0);//内角が180度未満の場合、採用。
                bot.push(P[i]);
                break;
            }
            else if (bot.size() == 1) {
                bot.push(P[i]);
                break;
            }
        }
    }
    while (bot.top() == top.top()) {
        bot.pop();
    }
    vector<pair<long long, long long>>R;
    while (!top.empty()) {
        R.push_back(top.top());
        top.pop();
    }
    reverse(R.begin(), R.end());
    while (!bot.empty()) {
        R.push_back(bot.top());
        bot.pop();
    }
    //Rには時計回り順に各頂点を訪問するときの経路(P[0]は始点及び終点とする)が入っている。
    //もし気になるなら全部チェックして内角を測り180度未満なことを確認してもよい。
    if (R.size() == N + 1) {//外周を凸包で囲んだときに点が足りているか
        cout << "Yes\n";
    }
    else {
        cout << "No\n";
    }
    return 0;
}
0