結果

問題 No.742 にゃんにゃんにゃん 猫の挨拶
ユーザー YamaKasaYamaKasa
提出日時 2018-11-08 02:17:15
言語 Java21
(openjdk 21)
結果
AC  
実行時間 453 ms / 2,500 ms
コード長 946 bytes
コンパイル時間 3,526 ms
コンパイル使用メモリ 75,344 KB
実行使用メモリ 60,584 KB
最終ジャッジ日時 2023-08-13 04:15:30
合計ジャッジ時間 6,769 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
55,588 KB
testcase_01 AC 124 ms
55,620 KB
testcase_02 AC 122 ms
55,636 KB
testcase_03 AC 122 ms
55,736 KB
testcase_04 AC 133 ms
55,540 KB
testcase_05 AC 145 ms
55,968 KB
testcase_06 AC 185 ms
56,044 KB
testcase_07 AC 196 ms
56,260 KB
testcase_08 AC 203 ms
58,572 KB
testcase_09 AC 121 ms
55,504 KB
testcase_10 AC 120 ms
55,684 KB
testcase_11 AC 453 ms
60,584 KB
testcase_12 AC 388 ms
60,500 KB
testcase_13 AC 122 ms
55,772 KB
testcase_14 AC 122 ms
55,668 KB
testcase_15 AC 126 ms
53,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int []a = new int[n];
		List<Integer> list = new ArrayList<Integer>();
		for(int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
			list.add(a[i]);
		}
		sc.close();
		Collections.sort(list);
		BIT bitC = new BIT(n);
		int ans = 0;
		for(int i = 0; i < n; i++) {
			a[i] = Collections.binarySearch(list, a[i]) + 1;
			ans += i - bitC.sum(a[i]);
			bitC.add(a[i], 1);
		}
		System.out.println(ans);
	}
	static class BIT{
		int []bit;
		int n;
		public BIT(int n) {
			this.n = n;
			bit = new int[n + 1];
		}
		int sum(int i) {
			int sum = 0;
			while(i > 0) {
				sum += bit[i];
				i -= i & -i;
			}
			return sum;
		}
		void add(int i, int x) {
			while(i <= n) {
				bit[i] += x;
				i += i & -i;
			}
		}
	}
}
0