提出 #9429060


ソースコード 拡げる

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
template<int SZ> struct Counter {
	int Count[SZ];
	set<int> Kinds;
	Counter() { rep(i, 0, SZ) Count[i] = 0; }

	void Increment(int i) {
		if (Count[i] == 0) Kinds.insert(i);
		Count[i]++;
	}

	void Decrement(int i) {
		Count[i]--;
		if (Count[i] == 0) Kinds.erase(i);
	}
};
/*---------------------------------------------------------------------------------------------------
            ∧_∧
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     @hamayanhamayan0
    /   \     | |
    /   / ̄ ̄ ̄ ̄/  |
  __(__ニつ/     _/ .| .|____
     \/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/














int N, A[101010];
int limit = 8;
//---------------------------------------------------------------------------------------------------
void _main() {
	cin >> N;
	rep(i, 0, N) cin >> A[i];
	rep(i, 0, N) A[i]--;

	int originalN = N;

	vector<int> ans;
	set<int> unused;
	rep(i, 0, N) unused.insert(i);
	
	Counter<101010> counter;
	rep(i, 0, N) counter.Increment(A[i]);

	while (limit < N) {
		if (counter.Kinds.size() == 2) {
			auto ite = counter.Kinds.begin();

			int x = *ite; ite++;
			int y = *ite;

			if (counter.Count[x] == N - 1) {
				if (unused.count(x)) {
					ans.push_back(x);
					N--;
					unused.erase(x);
					counter.Decrement(A[x]);
					continue;
				}
			}

			if (counter.Count[y] == N - 1) {
				if (unused.count(y)) {
					ans.push_back(y);
					N--;
					unused.erase(y);
					counter.Decrement(A[y]);
					continue;
				}
			}
		}

		auto ite = unused.begin();

		if (0 < ans.size() and A[ans.back()] == *ite) ite++;
		ans.push_back(*ite);
		N--;
		counter.Decrement(A[*ite]);
		unused.erase(*ite);
	}

	vector<int> rest;
	rep(i, 0, N) {
		int x = *unused.begin();
		rest.push_back(x);
		unused.erase(x);
	}

	do {
		if (0 < ans.size() and A[ans.back()] == rest[0]) continue;
		bool ok = true;
		rep(i, 0, N - 1) if (A[rest[i]] == rest[i + 1]) ok = false;
		if (ok) {
			rep(i, 0, N) ans.push_back(rest[i]);
			break;
		}
	} while (next_permutation(all(rest)));

	if (ans.size() != originalN) {
		cout << -1 << endl;
		return;
	}

	rep(i, 0, originalN) {
		if(i) printf(" ");
		printf("%d", ans[i] + 1);
	}
	printf("\n");
}





提出情報

提出日時
問題 D - Arrangement
ユーザ hamayanhamayan
言語 C++14 (GCC 5.4.1)
得点 800
コード長 3208 Byte
結果 AC
実行時間 86 ms
メモリ 11008 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 800 / 800
結果
AC × 3
AC × 50
セット名 テストケース
Sample sample_01, sample_02, sample_03
All hand2_01, hand2_02, hand2_03, hand2_04, hand_01, hand_02, hand_03, hand_04, handmaid_01, max_01, max_02, max_03, max_04, max_05, random_01, random_02, random_03, random_04, random_05, random_06, random_07, random_08, random_09, random_10, random_11, random_12, random_13, random_14, random_15, sample_01, sample_02, sample_03, small_01, small_02, small_03, small_04, small_05, special2_01, special2_02, special2_03, special2_04, special2_05, special2_06, special2_07, special2_08, special2_09, special2_10, special_01, special_02, special_03
ケース名 結果 実行時間 メモリ
hand2_01 AC 1 ms 640 KiB
hand2_02 AC 1 ms 640 KiB
hand2_03 AC 47 ms 6272 KiB
hand2_04 AC 47 ms 6272 KiB
hand_01 AC 1 ms 640 KiB
hand_02 AC 1 ms 640 KiB
hand_03 AC 86 ms 9216 KiB
hand_04 AC 85 ms 9216 KiB
handmaid_01 AC 1 ms 640 KiB
max_01 AC 76 ms 11008 KiB
max_02 AC 73 ms 10624 KiB
max_03 AC 66 ms 9472 KiB
max_04 AC 74 ms 10880 KiB
max_05 AC 50 ms 7168 KiB
random_01 AC 1 ms 640 KiB
random_02 AC 1 ms 640 KiB
random_03 AC 1 ms 640 KiB
random_04 AC 1 ms 640 KiB
random_05 AC 1 ms 640 KiB
random_06 AC 2 ms 640 KiB
random_07 AC 1 ms 640 KiB
random_08 AC 1 ms 640 KiB
random_09 AC 1 ms 640 KiB
random_10 AC 1 ms 640 KiB
random_11 AC 1 ms 640 KiB
random_12 AC 1 ms 640 KiB
random_13 AC 1 ms 640 KiB
random_14 AC 1 ms 640 KiB
random_15 AC 1 ms 640 KiB
sample_01 AC 1 ms 640 KiB
sample_02 AC 1 ms 640 KiB
sample_03 AC 1 ms 640 KiB
small_01 AC 1 ms 640 KiB
small_02 AC 1 ms 640 KiB
small_03 AC 1 ms 640 KiB
small_04 AC 1 ms 640 KiB
small_05 AC 2 ms 640 KiB
special2_01 AC 47 ms 6272 KiB
special2_02 AC 49 ms 6272 KiB
special2_03 AC 47 ms 6272 KiB
special2_04 AC 47 ms 6272 KiB
special2_05 AC 46 ms 6272 KiB
special2_06 AC 50 ms 6656 KiB
special2_07 AC 50 ms 6784 KiB
special2_08 AC 47 ms 6400 KiB
special2_09 AC 50 ms 6784 KiB
special2_10 AC 48 ms 6528 KiB
special_01 AC 1 ms 640 KiB
special_02 AC 3 ms 1024 KiB
special_03 AC 48 ms 6784 KiB