結果

問題 No.1051 PQ Permutation
ユーザー 37zigen37zigen
提出日時 2020-05-09 16:16:04
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,740 ms / 2,000 ms
コード長 4,309 bytes
コンパイル時間 3,049 ms
コンパイル使用メモリ 78,556 KB
実行使用メモリ 80,080 KB
最終ジャッジ日時 2023-09-19 09:06:46
合計ジャッジ時間 28,835 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
52,948 KB
testcase_01 AC 84 ms
52,652 KB
testcase_02 AC 45 ms
49,560 KB
testcase_03 AC 83 ms
52,568 KB
testcase_04 AC 84 ms
53,200 KB
testcase_05 AC 84 ms
52,860 KB
testcase_06 AC 82 ms
53,180 KB
testcase_07 AC 45 ms
49,236 KB
testcase_08 AC 81 ms
52,668 KB
testcase_09 AC 46 ms
49,540 KB
testcase_10 AC 84 ms
52,720 KB
testcase_11 AC 84 ms
52,516 KB
testcase_12 AC 84 ms
51,136 KB
testcase_13 AC 87 ms
52,596 KB
testcase_14 AC 83 ms
53,260 KB
testcase_15 AC 1,740 ms
79,772 KB
testcase_16 AC 1,331 ms
72,944 KB
testcase_17 AC 939 ms
62,204 KB
testcase_18 AC 938 ms
63,408 KB
testcase_19 AC 873 ms
62,776 KB
testcase_20 AC 1,341 ms
70,420 KB
testcase_21 AC 1,341 ms
70,472 KB
testcase_22 AC 917 ms
63,316 KB
testcase_23 AC 1,313 ms
71,600 KB
testcase_24 AC 1,378 ms
71,528 KB
testcase_25 AC 344 ms
59,088 KB
testcase_26 AC 269 ms
55,300 KB
testcase_27 AC 162 ms
54,424 KB
testcase_28 AC 379 ms
62,628 KB
testcase_29 AC 271 ms
55,336 KB
testcase_30 AC 350 ms
60,152 KB
testcase_31 AC 197 ms
55,108 KB
testcase_32 AC 344 ms
62,364 KB
testcase_33 AC 171 ms
54,144 KB
testcase_34 AC 166 ms
53,780 KB
testcase_35 AC 874 ms
71,136 KB
testcase_36 AC 697 ms
69,708 KB
testcase_37 AC 100 ms
51,616 KB
testcase_38 AC 1,279 ms
80,080 KB
testcase_39 AC 390 ms
67,940 KB
testcase_40 AC 958 ms
61,572 KB
testcase_41 AC 979 ms
69,868 KB
testcase_42 AC 86 ms
52,508 KB
testcase_43 AC 81 ms
53,024 KB
testcase_44 AC 80 ms
52,508 KB
testcase_45 AC 80 ms
53,116 KB
testcase_46 AC 80 ms
53,272 KB
testcase_47 AC 44 ms
49,688 KB
testcase_48 AC 414 ms
67,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.TreeSet;

public class Main{
	
	public static void main(String[] args) {
		new Main().solve();
	}
	
	void gen() {
		int N=200000;
		int P=1;
		int Q=N;
		int[] a=new int[N];
		for (int i=0;i<N-2;++i) {
			a[i]=N-1-i;
		}
		a[N-2]=N;
		a[N-1]=1;
		System.out.println(N+" "+P+" "+Q);
		for (int i=0;i<N;++i) {
			System.out.print(a[i]+(i==N-1?"\n":" "));
		}
	}
	
	boolean check(int[] A,int P,int Q) {
		return find(A, P) < find(A, Q);
	}
	
	void yes(int[] A) {
		for (int i=0;i<A.length;++i) {
			System.out.print(A[i]+(i==A.length-1?"\n":" "));
		}
		System.exit(0);
	}

	void no() {
		System.out.println(-1);
		System.exit(0);
	}
	
	int find(int[] A,int key) {
		for (int i=A.length-1;i>=0;--i) if (A[i]==key) return i;
		throw new AssertionError();
	}
	
	void solve() {
		FastScanner sc=new FastScanner();
		int N=sc.nextInt();
		int P=sc.nextInt();
		int Q=sc.nextInt();
		int[] A=new int[N];
		for (int i=0;i<N;++i) A[i]=sc.nextInt();
		if (!nextPermutation(A)) no();
		if (check(A, P, Q)) yes(A);
		while (true) {
			TreeSet<Integer> ts=new TreeSet<>();
			int max=0;
			int iq=find(A, Q);
			for (int i=N-1;i>=iq;--i) {
				ts.add(A[i]);
				max=Math.max(max, A[i]);
			}
			if (max==Q) {
				while (iq>=0 && (ts.ceiling(A[iq]+1)==null || ts.ceiling(A[iq]+1)==Q)) {
					--iq; 
					if (iq>=0) ts.add(A[iq]);
				}
			}
			Arrays.sort(A, iq+1, N);
			{
				int s=iq+1;
				int t=N-1;
				while (s<t) {
					A[s]^=A[t];A[t]^=A[s];A[s]^=A[t];
					++s;--t;
				}
			}
			if (!nextPermutation(A)) no();
			if (check(A, P, Q)) yes(A);
			if (!(iq>0&&A[iq-1]==Q)) break;
		}
		
		int ip=find(A, P);
		int iq=find(A, Q);
		for (int i=iq;i<ip;++i) {
			A[i]^=A[i+1];A[i+1]^=A[i];A[i]^=A[i+1];
		}
		//1,2,...,q,...,p
		//1,2,...,q+1,...,p,q
		if (check(A, P, Q)) yes(A);
		throw new AssertionError();
	}
	
	boolean nextPermutation(int[] a) {
		int n=a.length;
		int p=n-1;
		while (p>0 && a[p-1]>a[p]) --p;
		if (p==0) return false;
		int q=p;
		while (q+1<n && a[p-1]<a[q+1]) ++q;
		{
			a[p-1]^=a[q];a[q]^=a[p-1];a[p-1]^=a[q];
		}
		int s=p,t=n-1;
		while (s<t) {
			a[s]^=a[t];a[t]^=a[s];a[s]^=a[t];
			++s;--t;
		}
		return true;
	}
	
	void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));}
}

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
    public boolean hasNext() { skipUnprintable(); return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
    	return (int)nextLong();
    }
}
0