結果

問題 No.241 出席番号(1)
ユーザー izuru_matsuuraizuru_matsuura
提出日時 2016-08-23 22:24:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,696 bytes
コンパイル時間 1,572 ms
コンパイル使用メモリ 153,400 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-20 17:55:22
合計ジャッジ時間 3,020 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

namespace {

    typedef double real;
    typedef long long ll;

    template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
        for (auto& v : vs) {
            os << v << endl;
        }
        return os;
    }
    template<class T> istream& operator>>(istream& is, vector<T>& vs) {
        for (auto it = vs.begin(); it != vs.end(); it++) is >> *it;
        return is;
    }

    int N;
    vector<int> A;
    void input() {
        cin >> N;
        A.clear(); A.resize(N); cin >> A;
    }

    void solve() {
        vector<bool> ng_exist(N, false);
        vector<vector<int>> ng_id(N);
        for (int i = 0; i < N; i++) {
            if (A[i] >= N) continue;
            ng_id[A[i]].push_back(i);
            ng_exist[A[i]] = true;
        }
        vector<int> wildcard;
        for (int i = 0; i < N; i++) if (not ng_exist[i]) wildcard.push_back(i);

        auto check = [&] {
            for (int i = 0; i < N; i++) {
                if (int(ng_id[i].size()) == N) return true;
            }
            return false;
        };

        if (check()) {
            int x = 0;
            for (int j = 0; j < N; j++) {
                if (ng_exist[j]) {
                    x = j;
                    break;
                }
            }
            if (x < N) {
                cout << -1 << endl;
                return;
            }
        }

        if (int(wildcard.size()) == N) {
            for (int i = 0; i < N; i++) {
                cout << i << endl;
            }
            return;
        }

        //cout << "ng_id: " << ng_id << endl;
        //cout << "wildcard: " << wildcard << endl;

        vector<int> id(N, -10);
        for (int i = 0; i < N; i++) {
            if (ng_id[i].empty()) continue;
            int x = ng_id[i][0];
            int next = (i + 1) % N;
            while (ng_id[next].empty()) {
                next = (next + 1) % N;
                if (i == next) break;
            }
            if (i == next) {
                id[x] = wildcard.back();
                wildcard.pop_back();
            } else {
                id[x] = next;
            }
            for (int j = 1; j < int(ng_id[i].size()); j++) {
                id[ng_id[i][j]] = wildcard.back();
                wildcard.pop_back();
            }
            if (i == next) {
                wildcard.push_back(i);
            }
        }
        for (int i = 0; i < N; i++) {
            if (id[i] < 0) {
                id[i] = wildcard.back();
                wildcard.pop_back();
            }
        }
        cout << id << endl;
    }
}

int main() {
    input(); solve();
    return 0;
}

0