結果
問題 | No.2665 Minimize Inversions of Deque |
ユーザー | tenten |
提出日時 | 2024-03-12 11:29:28 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 564 ms / 2,000 ms |
コード長 | 2,924 bytes |
コンパイル時間 | 2,518 ms |
コンパイル使用メモリ | 93,416 KB |
実行使用メモリ | 67,212 KB |
最終ジャッジ日時 | 2024-09-29 22:15:08 |
合計ジャッジ時間 | 20,261 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 101 ms
39,908 KB |
testcase_01 | AC | 427 ms
50,312 KB |
testcase_02 | AC | 426 ms
52,800 KB |
testcase_03 | AC | 413 ms
50,048 KB |
testcase_04 | AC | 394 ms
49,864 KB |
testcase_05 | AC | 392 ms
50,156 KB |
testcase_06 | AC | 398 ms
50,300 KB |
testcase_07 | AC | 392 ms
50,276 KB |
testcase_08 | AC | 392 ms
49,900 KB |
testcase_09 | AC | 386 ms
50,216 KB |
testcase_10 | AC | 418 ms
50,136 KB |
testcase_11 | AC | 420 ms
49,404 KB |
testcase_12 | AC | 431 ms
50,000 KB |
testcase_13 | AC | 415 ms
49,776 KB |
testcase_14 | AC | 383 ms
50,000 KB |
testcase_15 | AC | 421 ms
50,072 KB |
testcase_16 | AC | 386 ms
48,368 KB |
testcase_17 | AC | 387 ms
50,052 KB |
testcase_18 | AC | 411 ms
49,852 KB |
testcase_19 | AC | 256 ms
48,104 KB |
testcase_20 | AC | 133 ms
42,340 KB |
testcase_21 | AC | 131 ms
40,776 KB |
testcase_22 | AC | 136 ms
41,996 KB |
testcase_23 | AC | 149 ms
41,884 KB |
testcase_24 | AC | 138 ms
41,844 KB |
testcase_25 | AC | 139 ms
42,212 KB |
testcase_26 | AC | 134 ms
40,796 KB |
testcase_27 | AC | 145 ms
42,780 KB |
testcase_28 | AC | 152 ms
41,912 KB |
testcase_29 | AC | 138 ms
41,944 KB |
testcase_30 | AC | 539 ms
67,212 KB |
testcase_31 | AC | 465 ms
55,520 KB |
testcase_32 | AC | 486 ms
57,184 KB |
testcase_33 | AC | 564 ms
63,992 KB |
testcase_34 | AC | 556 ms
56,552 KB |
testcase_35 | AC | 509 ms
63,808 KB |
testcase_36 | AC | 528 ms
63,724 KB |
testcase_37 | AC | 482 ms
57,804 KB |
testcase_38 | AC | 552 ms
64,336 KB |
testcase_39 | AC | 533 ms
64,904 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int t = sc.nextInt(); String result = IntStream.range(0, t).mapToObj(p -> { int n = sc.nextInt(); BinaryIndexedTree bit = new BinaryIndexedTree(n + 1); Deque<Integer> current = new ArrayDeque<>(); int tmp = sc.nextInt(); current.addLast(tmp); bit.add(tmp, 1); long count = 0; for (int i = 1; i < n; i++) { int x = sc.nextInt(); int left = bit.getSum(x); int right = i - left; if (left < right || (left == right && current.peek() > x)) { current.addFirst(x); count += left; } else { current.addLast(x); count += right; } bit.add(x, 1); } return count + "\n" + current.stream().map(x -> x.toString()) .collect(Collectors.joining(" ")); }).collect(Collectors.joining("\n")); System.out.println(result); } } class BinaryIndexedTree { int size; int[] tree; public BinaryIndexedTree(int size) { this.size = size; tree = new int[size]; } public void add(int idx, int value) { int mask = 1; while (idx < size) { if ((idx & mask) != 0) { tree[idx] += value; idx += mask; } mask <<= 1; } } public int getSum(int from, int to) { return getSum(to) - getSum(from - 1); } public int getSum(int x) { int mask = 1; int ans = 0; while (x > 0) { if ((x & mask) != 0) { ans += tree[x]; x -= mask; } mask <<= 1; } return ans; } } class Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { try { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } finally { return st.nextToken(); } } }