結果

問題 No.241 出席番号(1)
ユーザー izuru_matsuura
提出日時 2016-08-23 22:24:53
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,696 bytes
コンパイル時間 1,534 ms
コンパイル使用メモリ 166,640 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-30 21:36:18
合計ジャッジ時間 2,449 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0