結果

問題 No.326 あみだますたー
ユーザー 37zigen37zigen
提出日時 2016-05-02 21:57:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 400 ms / 2,000 ms
コード長 1,329 bytes
コンパイル時間 2,517 ms
コンパイル使用メモリ 80,164 KB
実行使用メモリ 48,780 KB
最終ジャッジ日時 2024-04-25 12:47:27
合計ジャッジ時間 11,595 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
41,464 KB
testcase_01 AC 151 ms
42,112 KB
testcase_02 AC 216 ms
43,140 KB
testcase_03 AC 383 ms
48,780 KB
testcase_04 AC 240 ms
44,464 KB
testcase_05 AC 138 ms
41,372 KB
testcase_06 AC 319 ms
48,220 KB
testcase_07 AC 213 ms
44,392 KB
testcase_08 AC 295 ms
46,204 KB
testcase_09 AC 400 ms
48,624 KB
testcase_10 AC 396 ms
48,152 KB
testcase_11 AC 176 ms
42,848 KB
testcase_12 AC 337 ms
48,220 KB
testcase_13 AC 339 ms
48,712 KB
testcase_14 AC 269 ms
44,120 KB
testcase_15 AC 291 ms
47,108 KB
testcase_16 AC 294 ms
46,808 KB
testcase_17 AC 313 ms
46,188 KB
testcase_18 AC 303 ms
47,308 KB
testcase_19 AC 319 ms
48,356 KB
testcase_20 AC 161 ms
42,504 KB
testcase_21 AC 159 ms
42,000 KB
testcase_22 AC 334 ms
48,300 KB
testcase_23 AC 336 ms
47,896 KB
testcase_24 AC 357 ms
48,536 KB
testcase_25 AC 354 ms
48,668 KB
testcase_26 AC 245 ms
46,512 KB
testcase_27 AC 337 ms
47,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
	public static void main(String[] args)throws Exception{
		new Main().solve();
	}
	void solve(){
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int k=sc.nextInt();

		int[] d=new int[n];
		for(int i=0;i<n;i++){
			d[i]=i;
		}
		for(int i=0;i<k;i++){
			int x=sc.nextInt()-1;
			int y=sc.nextInt()-1;
			d=Arrays.copyOf(swap(d,x,y), d.length);
		}
		//テスト用
		//			tr(d);


		ArrayDeque<Pair> q=new ArrayDeque<Pair>();
		int[] ab=new int[n];
		for(int i=0;i<n;i++){
			ab[sc.nextInt()-1]=i;
		}
		for(int i=0;i<n;i++){
			int a=ab[i];
			if(d[i]==a)continue;
			for(int j=i+1;j<n;j++){
				if(d[j]==a){
					a=j;
					break;
				}
			}
			for(int j=a;j>i;j--){
				d=Arrays.copyOf(swap(d,j,j-1), d.length);
				q.addLast(new Pair(j-1,j));
			}
		}
		System.out.println(q.size());
		while(!q.isEmpty()){
			Pair p=q.pollFirst();
			System.out.println((p.a+1)+" "+(p.b+1));
		}
	}
	class Pair{
		//a<bとする
		int a;
		int b;
		Pair(int a,int b){
			if(a>b){
				int d=a;
				a=b;
				b=d;
			}
			this.a=a;
			this.b=b;
		}
	}
	int[] swap(int[] d,int a,int b){
		int dumy=d[a];
		d[a]=d[b];
		d[b]=dumy;
		return d;
	}
	void tr(Object...o){System.out.println(Arrays.deepToString(o));}
}

0