結果

問題 No.694 square1001 and Permutation 3
ユーザー 37zigen37zigen
提出日時 2018-05-24 01:16:10
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,749 ms / 3,000 ms
コード長 3,434 bytes
コンパイル時間 3,169 ms
コンパイル使用メモリ 75,532 KB
実行使用メモリ 105,380 KB
最終ジャッジ日時 2023-09-11 02:10:29
合計ジャッジ時間 13,850 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,548 KB
testcase_01 AC 43 ms
49,452 KB
testcase_02 AC 43 ms
49,420 KB
testcase_03 AC 51 ms
49,764 KB
testcase_04 AC 51 ms
49,448 KB
testcase_05 AC 51 ms
49,552 KB
testcase_06 AC 50 ms
49,884 KB
testcase_07 AC 1,048 ms
105,380 KB
testcase_08 AC 1,637 ms
104,708 KB
testcase_09 AC 1,749 ms
104,580 KB
testcase_10 AC 561 ms
84,360 KB
testcase_11 AC 1,721 ms
104,036 KB
testcase_12 AC 1,684 ms
104,392 KB
testcase_13 AC 43 ms
49,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.NoSuchElementException;
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() {
		FastScanner sc = new FastScanner();
		int N = (int) sc.nextLong();
		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));
	}

	class FastScanner {
		private final InputStream in = System.in;
		private final byte[] buffer = new byte[1024];
		private int ptr = 0;
		private int buflen = 0;

		private boolean hasNextByte() {
			if (ptr < buflen) {
				return true;
			} else {
				ptr = 0;
				try {
					buflen = in.read(buffer);
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (buflen <= 0) {
					return false;
				}
			}
			return true;
		}

		private int readByte() {
			if (hasNextByte())
				return buffer[ptr++];
			else
				return -1;
		}

		private boolean isPrintableChar(int c) {
			return 33 <= c && c <= 126;
		}

		private void skipUnprintable() {
			while (hasNextByte() && !isPrintableChar(buffer[ptr]))
				ptr++;
		}

		public boolean hasNext() {
			skipUnprintable();
			return hasNextByte();
		}

		public String next() {
			if (!hasNext())
				throw new NoSuchElementException();
			StringBuilder sb = new StringBuilder();
			int b = readByte();
			while (isPrintableChar(b)) {
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		public long nextLong() {
			if (!hasNext())
				throw new NoSuchElementException();
			long n = 0;
			boolean minus = false;
			int b = readByte();
			if (b == '-') {
				minus = true;
				b = readByte();
			}
			if (b < '0' || '9' < b) {
				throw new NumberFormatException();
			}
			while (true) {
				if ('0' <= b && b <= '9') {
					n *= 10;
					n += b - '0';
				} else if (b == -1 || !isPrintableChar(b)) {
					return minus ? -n : n;
				} else {
					throw new NumberFormatException();
				}
				b = readByte();
			}
		}
	}

}
0