結果
問題 | No.694 square1001 and Permutation 3 |
ユーザー | 37zigen |
提出日時 | 2018-05-24 01:16:10 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,646 ms / 3,000 ms |
コード長 | 3,434 bytes |
コンパイル時間 | 2,316 ms |
コンパイル使用メモリ | 80,308 KB |
実行使用メモリ | 89,912 KB |
最終ジャッジ日時 | 2024-06-28 16:41:45 |
合計ジャッジ時間 | 13,576 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 51 ms
36,844 KB |
testcase_01 | AC | 50 ms
36,848 KB |
testcase_02 | AC | 52 ms
36,548 KB |
testcase_03 | AC | 55 ms
37,112 KB |
testcase_04 | AC | 57 ms
37,204 KB |
testcase_05 | AC | 56 ms
36,668 KB |
testcase_06 | AC | 55 ms
36,832 KB |
testcase_07 | AC | 1,092 ms
84,172 KB |
testcase_08 | AC | 1,495 ms
84,336 KB |
testcase_09 | AC | 1,629 ms
89,708 KB |
testcase_10 | AC | 576 ms
73,600 KB |
testcase_11 | AC | 1,629 ms
89,912 KB |
testcase_12 | AC | 1,646 ms
89,368 KB |
testcase_13 | AC | 53 ms
36,728 KB |
ソースコード
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(); } } } }