結果
| 問題 | No.1115 二つの数列 / Two Sequences |
| コンテスト | |
| ユーザー |
ks2m
|
| 提出日時 | 2020-07-17 21:39:35 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 347 ms / 2,000 ms |
| コード長 | 1,565 bytes |
| コンパイル時間 | 4,177 ms |
| コンパイル使用メモリ | 77,220 KB |
| 実行使用メモリ | 64,480 KB |
| 最終ジャッジ日時 | 2024-11-29 22:35:10 |
| 合計ジャッジ時間 | 12,605 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 35 |
ソースコード
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] sa = br.readLine().split(" ");
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(sa[i]) - 1;
}
sa = br.readLine().split(" ");
int[] b = new int[n];
for (int i = 0; i < n; i++) {
b[Integer.parseInt(sa[i]) - 1] = i + 1;
}
br.close();
int[] c = new int[n];
for (int i = 0; i < n; i++) {
c[i] = b[a[i]];
}
System.out.println(tentousuu(c));
}
static long tentousuu(int[] a) {
int n = a.length;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
min = Math.min(min, a[i]);
max = Math.max(max, a[i]);
}
min--;
int[] b = new int[n];
for (int i = 0; i < n; i++) {
b[i] = a[i] - min;
}
BIT bit = new BIT(max - min);
long ret = 0;
for (int i = 0; i < n; i++) {
ret += i - bit.sum(b[i]);
bit.add(b[i], 1);
}
return ret;
}
/**
* Binary Indexed Tree
* 要素数nで初期化
* add、sumは1-indexed
*/
static class BIT {
int n;
long[] arr;
public BIT(int n) {
this.n = n;
arr = new long[n + 1];
}
void add(int idx, long val) {
for (int i = idx; i <= n; i += i & -i) {
arr[i] += val;
}
}
long sum(int idx) {
long sum = 0;
for (int i = idx; i > 0; i -= i & -i) {
sum += arr[i];
}
return sum;
}
}
}
ks2m