結果

問題 No.1370 置換門松列
ユーザー boutarouboutarou
提出日時 2021-01-29 23:32:20
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 1,863 bytes
コンパイル時間 2,356 ms
コンパイル使用メモリ 213,876 KB
実行使用メモリ 9,664 KB
最終ジャッジ日時 2023-10-12 21:04:50
合計ジャッジ時間 4,607 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 1 ms
4,352 KB
testcase_17 AC 1 ms
4,352 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 1 ms
4,352 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 49 ms
9,480 KB
testcase_22 AC 33 ms
8,292 KB
testcase_23 AC 44 ms
8,792 KB
testcase_24 AC 16 ms
4,348 KB
testcase_25 AC 61 ms
9,600 KB
testcase_26 AC 60 ms
9,664 KB
testcase_27 AC 26 ms
8,184 KB
testcase_28 AC 26 ms
8,556 KB
testcase_29 AC 22 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < int(n); i++)
using ll = long long;
using P = pair<int, int>;

bool kadomatsu(ll a, ll b, ll c) {
    if (a != b && b != c && c != a && (max({a, b, c}) == b || min({a, b, c}) == b)) return true;
    return false;
}

bool kadomatsux(vector<int> &a) {
    rep(i, (int)a.size() - 2) {
        if (!kadomatsu(a[i], a[i + 1], a[i + 2])) {
            return false;
        }
    }
    return true;
}

int main() {
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    rep(i, n) {
        cin >> a[i];
        a[i]--;
    }
    vector<vector<int>> g(m);
    vector<int> h(m);
    rep(i, n - 1) {
        if (i % 2 == 0) {
            g[a[i]].push_back(a[i + 1]);
            h[a[i + 1]]++;
        }
        else {
            g[a[i + 1]].push_back(a[i]);
            h[a[i]]++;
        }
    }
    // rep(i, m) {
    //     cout << i << " " << h[i] << "=" << " ";
    //     rep(j, g[i].size()) {
    //         cout << g[i][j] << " ";
    //     }
    //     cout << endl;
    // }
    stack<int> st;
    rep(i, m) if (h[i] == 0) st.push(i);
    vector<P> ans;
    while (!st.empty()) {
        int now = st.top(); st.pop();
        ans.push_back({now, ans.size() + 1});
        for (auto ne : g[now]) {
            h[ne]--;
            if(h[ne] == 0) {
                st.push(ne);
            }
        }
    }
    if ((int)ans.size() != m) {
        cout << "No" << endl;
    }
    else {
        sort(ans.begin(), ans.end());
        vector<int> check;
        rep(i, n) {
            check.push_back(ans[a[i]].second);
        }
        if (!kadomatsux(check)) {
            cout << "No" << endl;
            return 0;
        }
        cout << "Yes" << endl;
        rep(i, m) {
            cout << ans[i].second << " ";
        }
        cout << endl;
    }
    return 0;
}
0