結果

問題 No.2665 Minimize Inversions of Deque
ユーザー tentententen
提出日時 2024-03-12 11:29:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 631 ms / 2,000 ms
コード長 2,924 bytes
コンパイル時間 6,852 ms
コンパイル使用メモリ 90,692 KB
実行使用メモリ 80,984 KB
最終ジャッジ日時 2024-03-12 11:29:58
合計ジャッジ時間 28,090 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
56,272 KB
testcase_01 AC 507 ms
63,744 KB
testcase_02 AC 469 ms
63,808 KB
testcase_03 AC 497 ms
63,536 KB
testcase_04 AC 488 ms
63,616 KB
testcase_05 AC 478 ms
63,528 KB
testcase_06 AC 463 ms
62,092 KB
testcase_07 AC 487 ms
63,656 KB
testcase_08 AC 485 ms
63,552 KB
testcase_09 AC 485 ms
63,908 KB
testcase_10 AC 482 ms
63,528 KB
testcase_11 AC 488 ms
63,692 KB
testcase_12 AC 480 ms
63,540 KB
testcase_13 AC 454 ms
63,456 KB
testcase_14 AC 475 ms
63,520 KB
testcase_15 AC 499 ms
63,628 KB
testcase_16 AC 504 ms
63,588 KB
testcase_17 AC 482 ms
63,276 KB
testcase_18 AC 516 ms
64,024 KB
testcase_19 AC 288 ms
62,732 KB
testcase_20 AC 146 ms
56,548 KB
testcase_21 AC 145 ms
56,428 KB
testcase_22 AC 160 ms
57,836 KB
testcase_23 AC 157 ms
57,696 KB
testcase_24 AC 152 ms
58,600 KB
testcase_25 AC 157 ms
57,832 KB
testcase_26 AC 149 ms
57,704 KB
testcase_27 AC 155 ms
58,736 KB
testcase_28 AC 155 ms
57,568 KB
testcase_29 AC 155 ms
57,572 KB
testcase_30 AC 631 ms
80,984 KB
testcase_31 AC 558 ms
69,032 KB
testcase_32 AC 567 ms
70,084 KB
testcase_33 AC 617 ms
79,712 KB
testcase_34 AC 601 ms
70,100 KB
testcase_35 AC 581 ms
77,884 KB
testcase_36 AC 587 ms
78,336 KB
testcase_37 AC 556 ms
70,984 KB
testcase_38 AC 602 ms
80,124 KB
testcase_39 AC 605 ms
80,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
        }
    }
    
}
0