結果

問題 No.1051 PQ Permutation
ユーザー 沙耶花沙耶花
提出日時 2020-05-08 22:23:01
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 280 ms / 2,000 ms
コード長 1,120 bytes
コンパイル時間 2,239 ms
コンパイル使用メモリ 206,852 KB
実行使用メモリ 14,256 KB
最終ジャッジ日時 2023-09-19 07:43:00
合計ジャッジ時間 7,387 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 213 ms
14,180 KB
testcase_16 AC 200 ms
13,428 KB
testcase_17 AC 42 ms
4,820 KB
testcase_18 AC 42 ms
4,876 KB
testcase_19 AC 41 ms
4,964 KB
testcase_20 AC 133 ms
10,564 KB
testcase_21 AC 138 ms
10,904 KB
testcase_22 AC 42 ms
4,940 KB
testcase_23 AC 134 ms
10,452 KB
testcase_24 AC 128 ms
10,436 KB
testcase_25 AC 8 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 9 ms
4,380 KB
testcase_31 AC 3 ms
4,376 KB
testcase_32 AC 9 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 77 ms
8,860 KB
testcase_36 AC 76 ms
8,800 KB
testcase_37 AC 135 ms
13,100 KB
testcase_38 AC 155 ms
14,256 KB
testcase_39 AC 124 ms
13,040 KB
testcase_40 AC 91 ms
9,628 KB
testcase_41 AC 85 ms
9,568 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 280 ms
13,628 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:29:23: 警告: ‘q’ may be used uninitialized [-Wmaybe-uninitialized]
   29 |                 if(p>q&&(!S.count(P)||!S.count(Q)))continue;
      |                    ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:13:15: 備考: ‘q’ はここで定義されています
   13 |         int p,q;
      |               ^
main.cpp:29:23: 警告: ‘p’ may be used uninitialized [-Wmaybe-uninitialized]
   29 |                 if(p>q&&(!S.count(P)||!S.count(Q)))continue;
      |                    ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:13:13: 備考: ‘p’ はここで定義されています
   13 |         int p,q;
      |             ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000



int main(){

	int N,P,Q;
	cin>>N>>P>>Q;
	int p,q;
	vector<int> A(N);
	for(int i=0;i<N;i++){
		scanf("%d",&A[i]);
		if(A[i]==P)p = i;
		if(A[i]==Q)q = i;
	}
	
	set<int> S;
	vector<int> ans;	
	
	for(int i=N-1;i>=0;i--){
		S.insert(A[i]);
		auto it= S.upper_bound(A[i]);
		L:
		if(S.count(P)&&!S.count(Q))continue;
		if(p>q&&(!S.count(P)||!S.count(Q)))continue;
		if(it==S.end())continue;
		if(S.count(P)&&(*it)==Q){
			it++;
			goto L;
		}

		for(int j=0;j<i;j++)ans.push_back(A[j]);
		ans.push_back(*it);
		S.erase(it);
		int t = -1;
		bool f = false;
		if(S.count(P))f=true;
		for(auto a:S){
			if(a==P){
				ans.push_back(a);
				if(t!=-1)ans.push_back(t);
				f=false;
			}
			else if(a==Q){
				if(f){
					t = Q;
				}
				else{
					ans.push_back(a);
				}
			}
			else{
				ans.push_back(a);
			}
		}
		break;
	}
	
	if(ans.size()==0)cout<<-1<<endl;
	else{
		for(int i=0;i<ans.size();i++){
			if(i!=0)cout<<' ';
			cout<<ans[i];
		}
		cout<<endl;
	}
	
	return 0;
}
0