結果

問題 No.199 星を描こう
ユーザー ctyl_0ctyl_0
提出日時 2015-09-21 06:34:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,573 bytes
コンパイル時間 701 ms
コンパイル使用メモリ 87,920 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-28 18:43:57
合計ジャッジ時間 1,813 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,384 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>

#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
typedef long long ll;

using namespace std;


int inputValue(){
    int a;
    cin >> a;
    return a;
};

void inputArray(int * p, int a){
    rep(i, a){
        cin >> p[i];
    }
};

void inputVector(vector<int> * p, int a){
    rep(i, a){
        int input;
        cin >> input;
        p -> push_back(input);
    }
}

template <typename T>
void output(T a, int precision) {
    if(precision > 0){
        cout << setprecision(precision)  << a << "\n";
    }
    else{
        cout << a << "\n";
    }
}

// 正なら反時計回り,負なら時計回り
int det(pair<int, int> a, pair<int, int> b){
    return a.first * b.second - a.second * b.first;
}

// v = q - p
pair<int, int> vec(pair<int, int> p, pair<int, int> q){
    return make_pair(q.first - p.first, q.second - p.second);
}

bool isCCW(pair<int, int> a1, pair<int, int> a2, pair<int, int> a3){
    pair<int, int> v1 = vec(a2, a1);
    pair<int, int> v2 = vec(a2, a3);
    
    if (det(v1, v2) >= 0) {
        return true;
    }
    return false;
}

vector<pair<int, int>> convex_hull(vector<pair<int, int>> v){
    if (v.size() < 3) {
        return v;
    }
    
    sort(v.begin(), v.end());
    
    // vh: 上部の凸 vl: 下部の凸
    vector<pair<int, int>> vh, vl;
    // x座標の昇順に2つvhに追加 降順に2つvlに追加
    vh.push_back(v[0]);
    vh.push_back(v[1]);
    vl.push_back(v[v.size() - 1]);
    vl.push_back(v[v.size() - 2]);
    
    // vh
    repd(i, 2, v.size()){
        for (int n = (int)vh.size(); n >= 2 && isCCW(vh[n - 2], vh[n - 1], v[i]); n--) {
            vh.pop_back();
        }
        vh.push_back(v[i]);
    }
    
    // vl
    for (int i = (int)v.size() - 3; i >= 0; i--) {
        for (int n = (int)vl.size(); n >= 2 && isCCW(vl[n - 2], vl[n - 1], v[i]); n--) {
            vl.pop_back();
        }
        vl.push_back(v[i]);
    }
    
    reverse(vl.begin(), vl.end());
    for (int i = (int)vh.size() - 2; i >= 1; i--) {
        vl.push_back(vh[i]);
    }
    
    return vl;
}

int main(int argc, const char * argv[]) {
    
    // source code
    vector<pair<int, int>> points(5);
    rep(i, 5){
        points[i] = make_pair(inputValue(), inputValue());
    }
    
    vector<pair<int, int>> star = convex_hull(points);
    
    output((star.size() == 5) ? "YES" : "NO", 0);
    
    return 0;
}
0