結果

問題 No.1051 PQ Permutation
ユーザー QCFiumQCFium
提出日時 2020-05-08 21:58:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,464 bytes
コンパイル時間 478 ms
コンパイル使用メモリ 47,620 KB
実行使用メモリ 11,480 KB
最終ジャッジ日時 2023-09-19 07:36:49
合計ジャッジ時間 4,836 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 TLE -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <algorithm>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}


#include <string.h>
void solve_gu(int n, int l, int r, int *a) {
	int perm[n];
	memcpy(perm, a, sizeof(int) * n);
	do {
		if (std::find(perm, perm + n, l) > std::find(perm, perm + n, r)) continue;
		for (auto i : perm) printf("%d ", i + 1);
		puts("");
		return;
	} while (std::next_permutation(perm, perm + n));
	puts("-1");
}

int main() {
	int n = ri();
	int l = ri() - 1;
	int r = ri() - 1;
	int a[n];
	for (auto &i : a) i = ri() - 1;
	solve_gu(n, l, r, a);
	if (!std::next_permutation(a, a + n)) {
		puts("-1");
		return 0;
	}
	int max[n];
	max[n - 1] = a[n - 1];
	for (int i = n; --i; ) max[i - 1] = std::max(max[i], a[i - 1]);
	int lpos = std::find(a, a + n, l) - a;
	int rpos = std::find(a, a + n, r) - a;
	if (lpos > rpos) {
		int i = rpos;
		while (i >= 0 && max[i] == a[i]) i--;
		if (i == -1) {
			puts("-1");
			return 0;
		}
		int min = 1000000000, index = -1;
		for (int j = i + 1; j < n; j++) if (a[j] > a[i] && min > a[j]) min = a[j], index = j;
		std::swap(a[i], a[index]);
		std::sort(a + i + 1, a + n);
		
		bool l_found = false;
		bool r_bury = false;
		for (auto j : a) {
			if (j == l) {
				l_found = true;
				printf("%d ", l + 1);
				if (r_bury) printf("%d ", r + 1);
			} else if (j == r && !l_found) r_bury = true;
			else printf("%d ", j + 1);
		}
		puts("");
	} else {
		for (auto i : a) printf("%d ", i + 1);
		puts("");
	}
	
	return 0;
}
0