結果

問題 No.1051 PQ Permutation
ユーザー QCFiumQCFium
提出日時 2020-05-08 21:58:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,464 bytes
コンパイル時間 529 ms
コンパイル使用メモリ 47,512 KB
実行使用メモリ 4,612 KB
最終ジャッジ日時 2023-09-19 07:36:17
合計ジャッジ時間 4,250 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 57 ms
4,572 KB
testcase_16 AC 57 ms
4,548 KB
testcase_17 AC 43 ms
4,588 KB
testcase_18 AC 43 ms
4,612 KB
testcase_19 AC 43 ms
4,584 KB
testcase_20 AC 51 ms
4,560 KB
testcase_21 AC 52 ms
4,480 KB
testcase_22 AC 43 ms
4,604 KB
testcase_23 AC 51 ms
4,536 KB
testcase_24 AC 51 ms
4,540 KB
testcase_25 AC 5 ms
4,376 KB
testcase_26 AC 4 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 7 ms
4,380 KB
testcase_29 AC 3 ms
4,380 KB
testcase_30 AC 6 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 6 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 21 ms
4,380 KB
testcase_36 AC 22 ms
4,380 KB
testcase_37 AC 23 ms
4,380 KB
testcase_38 AC 43 ms
4,520 KB
testcase_39 AC 24 ms
4,488 KB
testcase_40 AC 41 ms
4,592 KB
testcase_41 AC 42 ms
4,576 KB
testcase_42 WA -
testcase_43 AC 1 ms
4,384 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 1 ms
4,384 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 WA -
testcase_48 WA -
権限があれば一括ダウンロードができます

ソースコード

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);
	while (std::next_permutation(perm, perm + n)) {
		if (std::find(perm, perm + n, l) > std::find(perm, perm + n, r)) continue;
		for (auto i : perm) printf("%d ", i + 1);
		puts("");
		return;
	};
	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