結果

問題 No.1051 PQ Permutation
ユーザー kriiikriii
提出日時 2020-05-10 08:49:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 996 bytes
コンパイル時間 324 ms
コンパイル使用メモリ 44,076 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-21 09:50:46
合計ジャッジ時間 4,221 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <stdio.h>
#include <algorithm>
using namespace std;

int N, P, Q, A[200200], p, q; bool C[200200];

int main()
{
	scanf ("%d %d %d", &N, &P, &Q);
	for (int i = 0; i < N; i++) scanf ("%d", &A[i]);

	for (int k = 0; next_permutation(A, A + N); k++){
		for (int i = 0; i < N; i++){
			if (A[i] == P) p = i;
			if (A[i] == Q) q = i;
		}

		if (p < q){
			for (int i = 0; i < N; i++) printf ("%d ", A[i]);
			return 0;
		}

		if (k){
			for (int i = 0; i < q; i++) printf ("%d ", A[i]);
			for (int i = q + 1; i < p; i++) printf ("%d ", A[i]);
			printf ("%d %d ", P, Q);
			for (int i = p + 1; i < N; i++) printf ("%d ", A[i]);
			return 0;
		}
		else{
			int u = 0, w = Q;
			for (int i = q + 1; i < N; i++) u = max(u, A[i]);
			if (Q > u){
				for (q--; q >= 0; q--){
					if (w < A[q]) w = A[q];
					else if (u < A[q] && A[q] < Q) u = A[q];
					else break;
				}
			}

			for (int i = 1; i <= N; i++) if (C[i]) A[++q] = i;
			reverse(A + q + 1, A + N);
		}
	}

	puts("-1");
	return 0;
}
0