結果

問題 No.2665 Minimize Inversions of Deque
ユーザー ks2mks2m
提出日時 2024-03-08 21:39:47
言語 Java21
(openjdk 21)
結果
AC  
実行時間 566 ms / 2,000 ms
コード長 2,903 bytes
コンパイル時間 6,184 ms
コンパイル使用メモリ 80,296 KB
実行使用メモリ 81,084 KB
最終ジャッジ日時 2024-03-08 21:40:09
合計ジャッジ時間 20,509 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
54,116 KB
testcase_01 AC 358 ms
61,348 KB
testcase_02 AC 344 ms
66,352 KB
testcase_03 AC 352 ms
65,904 KB
testcase_04 AC 353 ms
65,856 KB
testcase_05 AC 353 ms
66,056 KB
testcase_06 AC 363 ms
61,168 KB
testcase_07 AC 349 ms
65,720 KB
testcase_08 AC 357 ms
65,904 KB
testcase_09 AC 357 ms
65,200 KB
testcase_10 AC 354 ms
65,692 KB
testcase_11 AC 352 ms
61,164 KB
testcase_12 AC 353 ms
65,892 KB
testcase_13 AC 353 ms
65,648 KB
testcase_14 AC 349 ms
65,836 KB
testcase_15 AC 350 ms
65,840 KB
testcase_16 AC 355 ms
65,664 KB
testcase_17 AC 353 ms
66,020 KB
testcase_18 AC 352 ms
61,200 KB
testcase_19 AC 208 ms
59,236 KB
testcase_20 AC 109 ms
55,536 KB
testcase_21 AC 97 ms
53,960 KB
testcase_22 AC 109 ms
55,652 KB
testcase_23 AC 95 ms
55,532 KB
testcase_24 AC 100 ms
53,696 KB
testcase_25 AC 110 ms
55,652 KB
testcase_26 AC 99 ms
55,652 KB
testcase_27 AC 99 ms
55,660 KB
testcase_28 AC 102 ms
55,416 KB
testcase_29 AC 101 ms
55,800 KB
testcase_30 AC 566 ms
81,084 KB
testcase_31 AC 421 ms
71,144 KB
testcase_32 AC 431 ms
71,272 KB
testcase_33 AC 500 ms
75,484 KB
testcase_34 AC 411 ms
70,292 KB
testcase_35 AC 548 ms
78,240 KB
testcase_36 AC 471 ms
78,464 KB
testcase_37 AC 393 ms
68,180 KB
testcase_38 AC 475 ms
69,140 KB
testcase_39 AC 489 ms
78,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Deque;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		PrintWriter pw = new PrintWriter(System.out);
		for (int z = 0; z < t; z++) {
			int n = Integer.parseInt(br.readLine());
			String[] sa = br.readLine().split(" ");
			int[] p = new int[n];
			for (int i = 0; i < n; i++) {
				p[i] = Integer.parseInt(sa[i]) - 1;
			}

			long ans = 0;
			Deque<Integer> que = new ArrayDeque<>();
			FenwickTree ft = new FenwickTree(n);
			for (int i = 0; i < n; i++) {
				long l = ft.sum(p[i]);
				long r = ft.sum(p[i], n);
				if (l < r) {
					que.addFirst(p[i]);
					ans += l;
				} else if (l > r) {
					que.addLast(p[i]);
					ans += r;
				} else {
					if (que.isEmpty()) {
						que.add(p[i]);
					} else if (p[i] < que.peekFirst()) {
						que.addFirst(p[i]);
						ans += l;
					} else {
						que.addLast(p[i]);
						ans += r;
					}
				}
				ft.add(p[i], 1);
			}

			pw.println(ans);
			StringBuilder sb = new StringBuilder();
			while (!que.isEmpty()) {
				sb.append(que.poll() + 1).append(' ');
			}
			sb.deleteCharAt(sb.length() - 1);
			pw.println(sb.toString());
		}
		pw.flush();
		br.close();
	}
}

class FenwickTree {
	private int n;
	private long[] data;

	/**
	 * 長さnの配列(a[0]~a[n-1])を作る。初期値は全て0。<br>
	 * O(n)
	 * 
	 * @param n 配列の長さ
	 */
	public FenwickTree(int n) {
		this.n = n;
		data = new long[n];
	}

	/**
	 * 初期データを元にFenwick Treeを構成する。<br>
	 * O(n)
	 * 
	 * @param data 初期データ
	 */
	public FenwickTree(long[] data) {
		this(data.length);
		build(data);
	}

	/**
	 * a[p] += x を行う。<br>
	 * O(log n)
	 * 
	 * @param p 加算位置(0≦p<n)
	 * @param x 加算値
	 */
	void add(int p, long x) {
		assert 0 <= p && p < n : "p=" + p;

		p++;
		while (p <= n) {
			data[p - 1] += x;
			p += p & -p;
		}
	}

	/**
	 * a[l] + ... + a[r-1] を返す。<br>
	 * O(log n)
	 * 
	 * @param l 開始位置(含む)    (0≦l≦r≦n)
	 * @param r 終了位置(含まない)(0≦l≦r≦n)
	 */
	long sum(int l, int r) {
		assert 0 <= l && l <= r && r <= n : "l=" + l + ", r=" + r;

		return sum(r) - sum(l);
	}

	/**
	 * a[0] + ... + a[r-1] を返す。<br>
	 * O(log n)
	 * 
	 * @param r 終了位置(含まない)(0≦r≦n)
	 */
	long sum(int r) {
		assert 0 <= r && r <= n : "r=" + r;

		long s = 0;
		while (r > 0) {
			s += data[r - 1];
			r -= r & -r;
		}
		return s;
	}

	private void build(long[] dat) {
		System.arraycopy(dat, 0, data, 0, n);
		for (int i = 1; i <= n; i++) {
			int p = i + (i & -i);
			if (p <= n) {
				data[p - 1] += data[i - 1];
			}
		}
	}
}
0