結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー tenten
提出日時 2020-11-20 16:26:11
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,350 ms / 2,000 ms
コード長 860 bytes
コンパイル時間 2,418 ms
コンパイル使用メモリ 78,596 KB
実行使用メモリ 71,532 KB
最終ジャッジ日時 2024-07-23 12:11:08
合計ジャッジ時間 27,584 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

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);
		long 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);
		}
		System.out.println(count);
   }
}

0