結果

問題 No.406 鴨等間隔の法則
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-07-03 21:13:25
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 1,703 bytes
コンパイル時間 801 ms
コンパイル使用メモリ 83,120 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-21 17:38:15
合計ジャッジ時間 3,249 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 43 ms
4,380 KB
testcase_05 AC 18 ms
4,380 KB
testcase_06 AC 18 ms
4,376 KB
testcase_07 AC 19 ms
4,380 KB
testcase_08 AC 42 ms
4,376 KB
testcase_09 AC 47 ms
4,380 KB
testcase_10 AC 21 ms
4,376 KB
testcase_11 AC 36 ms
4,380 KB
testcase_12 AC 25 ms
4,376 KB
testcase_13 AC 24 ms
4,380 KB
testcase_14 AC 38 ms
4,376 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 6 ms
4,380 KB
testcase_19 AC 5 ms
4,380 KB
testcase_20 AC 6 ms
4,376 KB
testcase_21 AC 8 ms
4,376 KB
testcase_22 AC 11 ms
4,376 KB
testcase_23 AC 26 ms
4,380 KB
testcase_24 AC 34 ms
4,380 KB
testcase_25 AC 47 ms
4,376 KB
testcase_26 AC 46 ms
4,376 KB
testcase_27 AC 39 ms
4,380 KB
testcase_28 AC 39 ms
4,376 KB
testcase_29 AC 46 ms
4,384 KB
testcase_30 AC 45 ms
4,384 KB
testcase_31 AC 45 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>  // std::stable_sortを用いる
#include <cassert>
#include <ciso646>  // 論理演算子等の代替表記を用いる
#include <cstdlib>  // EXIT_SUCCESSを用いる
#include <iostream>
#include <unordered_set>
#include <vector>


constexpr int MIN_N = 3;
constexpr int MAX_N = 100000;
constexpr int MIN_X = 0;
constexpr int MAX_X = 100000000;


int main() {
    /* 以下のassertは入力が制約を満たしていることを確認するためのもの
     解答の際にはなくてもよい */
    // 鴨の数Nを受け取る
    int n;
    std::cin >> n;
    assert(MIN_N <= n and n <= MAX_N);
    // 座標の列を受け取る
    std::vector<int> xs;
    for (decltype(n) i = 0; i < n; i++)
    {
        int x;
        std::cin >> x;
        xs.push_back(x);
        assert(MIN_X <= x and x <= MAX_X);
    }
    // 座標たちを昇順でソートする
    std::stable_sort(xs.begin(), xs.end());
    // 漸化式 d_j = x_{j + 1} - x_j に基づき
    // 階差数列 {d_j} (j = 0, 1, ..., N - 2) をつくる
    // 階差数列の項の順番は関係なく、
    // この数列の要素からなる集合の要素と大きさがわかればよいので、
    // いきなり集合に入れる
    std::unordered_set<int> ds;
    for (decltype(n) j = 0; j < n - 1; j++)
    {
        ds.insert(xs[j + 1] - xs[j]);
    }
    // 条件2および条件1について判定
    // 集合の要素数が1であり、かつその要素が0ではないことを確かめる
    if (ds.size() == 1 and ds.find(0) == ds.end())
    {
        std::cout << "YES" << std::endl;
    }
    else
    {
        std::cout << "NO" << std::endl;
    }
    return EXIT_SUCCESS;
}
0