結果

問題 No.1051 PQ Permutation
ユーザー kotatsugamekotatsugame
提出日時 2020-05-08 22:43:26
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 959 bytes
コンパイル時間 1,036 ms
コンパイル使用メモリ 70,520 KB
実行使用メモリ 4,476 KB
最終ジャッジ日時 2023-09-19 07:54:35
合計ジャッジ時間 5,451 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,384 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 90 ms
4,380 KB
testcase_16 AC 90 ms
4,384 KB
testcase_17 AC 75 ms
4,380 KB
testcase_18 AC 76 ms
4,380 KB
testcase_19 AC 75 ms
4,376 KB
testcase_20 AC 86 ms
4,380 KB
testcase_21 AC 86 ms
4,380 KB
testcase_22 AC 76 ms
4,380 KB
testcase_23 AC 84 ms
4,384 KB
testcase_24 AC 84 ms
4,376 KB
testcase_25 AC 8 ms
4,380 KB
testcase_26 AC 5 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 12 ms
4,380 KB
testcase_29 AC 5 ms
4,380 KB
testcase_30 AC 8 ms
4,376 KB
testcase_31 AC 3 ms
4,380 KB
testcase_32 AC 8 ms
4,376 KB
testcase_33 AC 3 ms
4,376 KB
testcase_34 AC 3 ms
4,376 KB
testcase_35 AC 38 ms
4,376 KB
testcase_36 AC 39 ms
4,380 KB
testcase_37 AC 56 ms
4,380 KB
testcase_38 AC 78 ms
4,380 KB
testcase_39 AC 57 ms
4,380 KB
testcase_40 AC 74 ms
4,380 KB
testcase_41 AC 76 ms
4,380 KB
testcase_42 WA -
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 1 ms
4,380 KB
testcase_47 WA -
testcase_48 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:15:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-Wreturn-type]
   15 | main()
      | ^~~~
main.cpp: 関数 ‘int index(int)’ 内:
main.cpp:9:1: 警告: 制御が非 void 関数の終りに到達しました [-Wreturn-type]
    9 | }
      | ^

ソースコード

diff #

#include<iostream>
#include<algorithm>
using namespace std;
int N,P,Q;
int A[2<<17];
int index(int X)
{
	for(int i=0;i<N;i++)if(A[i]==X)return i;
}
void output()
{
	for(int i=0;i<N;i++)cout<<A[i]<<(i+1==N?"":" ");
	cout<<endl;
}
main()
{
	cin>>N>>P>>Q;
	for(int i=0;i<N;i++)cin>>A[i];
	if(!next_permutation(A,A+N))
	{
		cout<<-1<<endl;
		return 0;
	}
	if(index(P)<index(Q));
	else
	{
		int inv=index(Q);
		int cummax=0;
		for(int i=N-1;i>inv;i--)cummax=max(cummax,A[i]);
		for(int i=inv;;i--)
		{
			if(i<0)
			{
				cout<<-1<<endl;
				return 0;
			}
			cummax=max(cummax,A[i]);
			if(cummax>A[i])
			{
				sort(A+i+1,A+N);
				reverse(A+i+1,A+N);
				next_permutation(A+i,A+N);
				int ip=index(P),iq=index(Q);
				if(iq==i)
				{
					sort(A+i+1,A+N);
					reverse(A+i+1,A+N);
					next_permutation(A+i,A+N);
					ip=index(P),iq=index(Q);
				}
				if(ip<iq)break;
				else
				{
					rotate(A+iq,A+iq+1,A+ip+1);
					break;
				}
			}
		}
	}
	output();
}
0