結果
問題 | No.1115 二つの数列 / Two Sequences |
ユーザー | sca1l |
提出日時 | 2020-07-19 14:06:28 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 571 ms / 2,000 ms |
コード長 | 2,095 bytes |
コンパイル時間 | 5,178 ms |
コンパイル使用メモリ | 74,284 KB |
実行使用メモリ | 64,428 KB |
最終ジャッジ日時 | 2023-08-22 09:55:33 |
合計ジャッジ時間 | 18,177 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 230 ms
59,628 KB |
testcase_01 | AC | 118 ms
54,300 KB |
testcase_02 | AC | 123 ms
56,232 KB |
testcase_03 | AC | 511 ms
63,628 KB |
testcase_04 | AC | 550 ms
64,392 KB |
testcase_05 | AC | 515 ms
63,808 KB |
testcase_06 | AC | 501 ms
63,800 KB |
testcase_07 | AC | 548 ms
64,376 KB |
testcase_08 | AC | 208 ms
59,464 KB |
testcase_09 | AC | 443 ms
60,640 KB |
testcase_10 | AC | 541 ms
63,760 KB |
testcase_11 | AC | 117 ms
55,568 KB |
testcase_12 | AC | 559 ms
64,428 KB |
testcase_13 | AC | 571 ms
64,104 KB |
testcase_14 | AC | 548 ms
64,312 KB |
testcase_15 | AC | 119 ms
55,996 KB |
testcase_16 | AC | 120 ms
55,676 KB |
testcase_17 | AC | 118 ms
56,084 KB |
testcase_18 | AC | 125 ms
55,612 KB |
testcase_19 | AC | 129 ms
55,620 KB |
testcase_20 | AC | 123 ms
56,056 KB |
testcase_21 | AC | 121 ms
55,520 KB |
testcase_22 | AC | 121 ms
55,620 KB |
testcase_23 | AC | 230 ms
59,804 KB |
testcase_24 | AC | 376 ms
60,824 KB |
testcase_25 | AC | 496 ms
62,372 KB |
testcase_26 | AC | 295 ms
60,556 KB |
testcase_27 | AC | 395 ms
60,644 KB |
testcase_28 | AC | 449 ms
60,616 KB |
testcase_29 | AC | 497 ms
63,384 KB |
testcase_30 | AC | 548 ms
64,016 KB |
testcase_31 | AC | 335 ms
60,364 KB |
testcase_32 | AC | 283 ms
60,460 KB |
testcase_33 | AC | 512 ms
64,056 KB |
testcase_34 | AC | 121 ms
56,196 KB |
testcase_35 | AC | 118 ms
55,536 KB |
testcase_36 | AC | 118 ms
55,796 KB |
testcase_37 | AC | 119 ms
55,804 KB |
testcase_38 | AC | 116 ms
55,872 KB |
testcase_39 | AC | 122 ms
55,632 KB |
ソースコード
import java.util.*; public class Main{ static final int MOD = (int)1e9+7; static HashMap<Integer, Integer> memo; public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.next()); int[] a = new int[n]; for(int i=0; i<n; i++){ a[i] = Integer.parseInt(sc.next()); } int[] b = new int[n+1]; for(int i=0; i<n; i++){ int idx = Integer.parseInt(sc.next()); b[idx] = i; } int[] c = new int[n]; for(int i=0; i<n; i++){ c[i] = b[a[i]]+1; } Inversion inv = new Inversion(); long ans = inv.inv(c); System.out.println(ans); } } class Inversion{ public long inv(int[] a){ long cnt = 0; int size = a.length; BinaryIndexedTree bit = new BinaryIndexedTree(size); for(int i=0; i<size; i++){ int p = a[i]; bit.add(p, 1); cnt += i + 1 - bit.sum(p); } return cnt; } //座圧(1-indexed) //使うとよい public static int[] shrink(int[] a){ int n = a.length; long[] b = new long[n]; for (int i = 0; i < n; i++){ b[i] = ((long)a[i] << 32) + i; } Arrays.sort(b); int[] ret = new int[n]; int p = 1;//1-indexed for (int i = 0; i < n; i++){ if (i > 0 && (b[i]>>32) != (b[i - 1]>>32)){ p++; } ret[(int)(b[i]>>32)] = p; } return ret; } } class BinaryIndexedTree{ int size; long[] tree; public BinaryIndexedTree(int n){ this.size = n; this.tree = new long[n+1]; } public long sum(int i){ long s = 0L; while(0<i){ s += tree[i]; i -= i & -i; } return s; } public void add(int i, int x){ while(i<=size){ tree[i] += x; i += i & -i; } } }