結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー tentententen
提出日時 2020-11-20 16:23:34
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,141 bytes
コンパイル時間 5,140 ms
コンパイル使用メモリ 74,372 KB
実行使用メモリ 71,068 KB
最終ジャッジ日時 2023-09-30 18:30:56
合計ジャッジ時間 8,589 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 450 ms
62,988 KB
testcase_01 AC 127 ms
55,728 KB
testcase_02 AC 135 ms
55,788 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] arr = new int[n];
		for (int i = 0; i < n; i++) {
		    arr[i] = sc.nextInt();
		}
		int[] order = new int[n + 1];
		for (int i = 0; i < n; i++) {
		    order[sc.nextInt()] = i;
		}
		ArrayList<Integer> list = new ArrayList<>();
		list.add(-1);
		list.add(Integer.MAX_VALUE);
		int count = 0;
		for (int i = 0; i < n; i++) {
		    int x = order[arr[i]];
		    int left = 0;
		    int right = list.size();
		    while (right - left > 1) {
		        int m = (left + right) / 2;
		        if (list.get(m) < x) {
		            left = m;
		        } else {
		            right = m;
		        }
		    }
		    count += list.size() - right - 1;
		    list.add(right, x);
		}
		count = 0;
		for (int i = n - 1; i > 0; i--) {
		    for (int j = 0; j < i; j++) {
		        if (order[arr[j]] > order[arr[j + 1]]) {
		            int tmp = arr[j];
		            arr[j] = arr[j + 1];
		            arr[j + 1] = tmp;
		            count++;
		        }
		    }
		}
		System.out.println(count);
   }
}

0