結果

問題 No.742 にゃんにゃんにゃん 猫の挨拶
ユーザー YamaKasa
提出日時 2018-11-08 02:17:15
言語 Java
(openjdk 23)
結果
AC  
実行時間 513 ms / 2,500 ms
コード長 946 bytes
コンパイル時間 2,760 ms
コンパイル使用メモリ 79,552 KB
実行使用メモリ 48,956 KB
最終ジャッジ日時 2024-11-20 20:57:43
合計ジャッジ時間 6,292 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

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