結果

問題 No.694 square1001 and Permutation 3
ユーザー 37zigen37zigen
提出日時 2018-05-24 01:08:49
言語 Java19
(openjdk 21)
結果
AC  
実行時間 2,774 ms / 3,000 ms
コード長 1,719 bytes
コンパイル時間 2,518 ms
コンパイル使用メモリ 76,160 KB
実行使用メモリ 109,336 KB
最終ジャッジ日時 2023-09-11 02:10:14
合計ジャッジ時間 20,099 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
55,540 KB
testcase_01 AC 125 ms
55,824 KB
testcase_02 AC 126 ms
55,796 KB
testcase_03 AC 191 ms
56,424 KB
testcase_04 AC 179 ms
56,004 KB
testcase_05 AC 193 ms
55,956 KB
testcase_06 AC 193 ms
56,444 KB
testcase_07 AC 1,755 ms
94,104 KB
testcase_08 AC 2,249 ms
95,112 KB
testcase_09 AC 2,774 ms
96,624 KB
testcase_10 AC 1,289 ms
95,044 KB
testcase_11 AC 2,625 ms
109,336 KB
testcase_12 AC 2,670 ms
95,376 KB
testcase_13 AC 125 ms
55,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
	class BIT {
		int n;
		int[] a;

		public BIT(int n_) {
			n = n_;
			a = new int[n + 1];
		}

		void add(int k, int add) {
			++k;
			while (k < a.length) {
				a[k] += add;
				k += k & -k;
			}
		}

		int sum(int k) {
			++k;
			int ret = 0;
			while (k > 0) {
				ret += a[k];
				k -= k & -k;
			}
			return ret;
		}

		int sum(int a, int b) {
			return sum(b - 1) - sum(a - 1);
		}
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long[] A = new long[N];
		for (int i = 0; i < N; ++i) {
			A[i] = sc.nextLong();
		}
		A = shrink(A);
		long sum = 0;
		BIT bit = new BIT(A.length);
		for (int i = 0; i < N; ++i) {
			sum += bit.sum((int) A[i] + 1, N);
			bit.add((int) A[i], 1);
		}
		PrintWriter pw = new PrintWriter(System.out);
		pw.println(sum);
		for (int i = 0; i < N - 1; i++) {
			sum -= bit.sum(0, (int) A[i]);
			sum += bit.sum((int) A[i] + 1, N);
			pw.println(sum);
		}
		pw.close();
	}

	long[] shrink(long[] A) {
		int N = A.length;
		long[] ret = new long[N];
		long[][] B = new long[N][2];
		for (int i = 0; i < N; ++i) {
			B[i][0] = i;
			B[i][1] = A[i];
		}
		Arrays.sort(B, new Comparator<long[]>() {
			@Override
			public int compare(long[] o1, long[] o2) {
				return Long.compare(o1[1], o2[1]);
			}
		});
		int p = 0;
		for (int i = 0; i < N; ++i) {
			ret[(int) B[i][0]] = p;
			if (i + 1 < N && B[i][1] == B[i + 1][1]) {
				continue;
			}
			++p;
		}
		return ret;
	}

	public static void main(String[] args) {
		new Main().run();
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
0