結果

問題 No.326 あみだますたー
コンテスト
ユーザー ぴろず
提出日時 2015-12-20 21:26:33
言語 Java
(openjdk 25.0.2)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
AC  
実行時間 202 ms / 2,000 ms
コード長 950 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,914 ms
コンパイル使用メモリ 85,464 KB
実行使用メモリ 51,152 KB
最終ジャッジ日時 2026-05-08 06:45:15
合計ジャッジ時間 7,085 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

package no326;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] p = new int[n];
		for(int i=0;i<n;i++) {
			p[i] = i + 1;
		}
		for(int i=0;i<k;i++) {
			int x = sc.nextInt() - 1;
			int y = sc.nextInt() - 1;
			int temp = p[x];
			p[x] = p[y];
			p[y] = temp;
		}
		int[] a = new int[n];
		for(int i=0;i<n;i++) {
			a[i] = sc.nextInt();
		}
		for(int i=0;i<n;i++) {
			p[i] = a[p[i]-1];
		}
//		System.out.println(Arrays.toString(p));
		ArrayList<Integer> ans = new ArrayList<>();
		//insertion sort
		for(int i=1;i<n;i++) {
			for(int j=i-1;j>=0;j--) {
				if (p[j] > p[j+1]) {
					int temp = p[j];
					p[j] = p[j+1];
					p[j+1] = temp;
					ans.add(j);
				}
			}
		}
		System.out.println(ans.size());
		for(int x:ans) {
			System.out.println((x + 1) + " " + (x + 2));
		}
	}

}
0