結果

問題 No.1051 PQ Permutation
ユーザー 👑 kmjpkmjp
提出日時 2020-05-08 22:52:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,580 bytes
コンパイル時間 2,703 ms
コンパイル使用メモリ 208,404 KB
実行使用メモリ 14,520 KB
最終ジャッジ日時 2023-09-19 07:56:50
合計ジャッジ時間 8,907 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 2 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 WA -
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 WA -
testcase_09 AC 2 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 WA -
testcase_14 AC 2 ms
4,380 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 96 ms
14,280 KB
testcase_18 AC 95 ms
14,336 KB
testcase_19 AC 96 ms
14,376 KB
testcase_20 AC 259 ms
14,360 KB
testcase_21 WA -
testcase_22 AC 96 ms
14,364 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 5 ms
4,384 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 13 ms
5,124 KB
testcase_29 AC 5 ms
4,384 KB
testcase_30 WA -
testcase_31 AC 4 ms
4,380 KB
testcase_32 AC 14 ms
4,384 KB
testcase_33 AC 3 ms
4,384 KB
testcase_34 AC 3 ms
4,384 KB
testcase_35 WA -
testcase_36 AC 103 ms
8,812 KB
testcase_37 AC 170 ms
14,248 KB
testcase_38 AC 186 ms
14,312 KB
testcase_39 AC 173 ms
14,284 KB
testcase_40 AC 136 ms
14,376 KB
testcase_41 AC 140 ms
14,520 KB
testcase_42 WA -
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 2 ms
4,384 KB
testcase_48 AC 228 ms
14,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,P,Q;
int A[202020],rev[202020];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>P>>Q;
	set<int> L,R;
	FOR(i,N) {
		cin>>A[i];
		rev[A[i]]=i;
		L.insert(i+1);
	}
	
	
	for(i=N-1;i>=0;i--) {
		L.erase(A[i]);
		R.insert(A[i]);
		auto it=R.lower_bound(A[i]+1);
		if(it==R.end()) continue;
		
		if(L.count(P)&&rev[P]<rev[Q]) {
			out:
			A[i]=*it;
			R.erase(it);
			
			for(j=i+1;j<N;j++) {
				A[j]=*R.begin();
				R.erase(R.begin());
			}
			
			
			FOR(i,N) cout<<A[i]<<" ";
			cout<<endl;
			return;
		}
		if(R.count(P)&&R.count(Q)) {
			if(*it==P) {
				goto out;
			}
			if(*it==Q) {
				it++;
				if(it==R.end()) continue;
			}
			
			A[i]=*it;
			R.erase(it);
			
			R.erase(Q);
			for(j=i+1;j<N;j++) {
				A[j]=*R.begin();
				R.erase(R.begin());
				if(A[j]==P) {
					j++;
					A[j]=Q;
				}
			}
			FOR(i,N) cout<<A[i]<<" ";
			cout<<endl;
			return;
			
		}
		
	}
	cout<<-1<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0