結果

問題 No.2519 Coins in Array
ユーザー ks2mks2m
提出日時 2023-10-27 22:36:08
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 4,154 bytes
コンパイル時間 2,829 ms
コンパイル使用メモリ 92,712 KB
実行使用メモリ 155,784 KB
最終ジャッジ日時 2023-10-27 22:36:36
合計ジャッジ時間 26,038 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
53,760 KB
testcase_01 AC 53 ms
53,680 KB
testcase_02 AC 57 ms
53,680 KB
testcase_03 AC 55 ms
53,768 KB
testcase_04 AC 52 ms
53,692 KB
testcase_05 AC 54 ms
53,676 KB
testcase_06 AC 54 ms
53,688 KB
testcase_07 AC 54 ms
53,672 KB
testcase_08 AC 58 ms
53,676 KB
testcase_09 WA -
testcase_10 AC 57 ms
53,764 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 55 ms
53,692 KB
testcase_15 AC 55 ms
53,764 KB
testcase_16 WA -
testcase_17 AC 56 ms
53,680 KB
testcase_18 AC 54 ms
53,684 KB
testcase_19 AC 65 ms
53,688 KB
testcase_20 AC 55 ms
54,284 KB
testcase_21 AC 63 ms
53,752 KB
testcase_22 AC 53 ms
53,676 KB
testcase_23 AC 1,209 ms
153,100 KB
testcase_24 AC 1,820 ms
146,776 KB
testcase_25 AC 57 ms
53,756 KB
testcase_26 AC 59 ms
53,684 KB
testcase_27 AC 1,636 ms
155,664 KB
testcase_28 AC 1,622 ms
155,584 KB
testcase_29 AC 1,546 ms
155,784 KB
testcase_30 AC 1,407 ms
149,004 KB
testcase_31 AC 466 ms
73,788 KB
testcase_32 AC 965 ms
111,020 KB
testcase_33 AC 1,137 ms
140,124 KB
testcase_34 AC 704 ms
95,816 KB
testcase_35 AC 1,092 ms
124,548 KB
testcase_36 AC 623 ms
80,988 KB
testcase_37 AC 361 ms
67,580 KB
testcase_38 AC 934 ms
110,712 KB
testcase_39 AC 656 ms
94,304 KB
testcase_40 AC 1,019 ms
147,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String[] sa = br.readLine().split(" ");
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(sa[i]);
		}
		br.close();

		PriorityQueue<Obj> que = new PriorityQueue<>((o1, o2) -> Long.compare(o1.b, o2.b));
		for (int i = 0; i < n; i++) {
			Obj o = new Obj();
			o.i = i;
			o.a = a[i];
			o.map = bunkai(o.a);
			o.b = minKey(o.map);
			que.add(o);
		}

		List<String> list = new ArrayList<>();
		FenwickTree ft = new FenwickTree(n * 2);
		int start = 0;
		if (n >= 4) {
			Obj o1 = que.poll();
			Obj o2 = que.poll();
			Obj o3 = que.poll();
			Obj o4 = que.poll();
			calc(o1, o2, list, ft, n, 0, que);
			calc(o3, o4, list, ft, n, 1, que);
			start = 2;
		}
		for (int i = start; i < n - 1; i++) {
			Obj o1 = que.poll();
			Obj o2 = que.poll();
			calc(o1, o2, list, ft, n, i, que);
		}
		PrintWriter pw = new PrintWriter(System.out);
		pw.println(que.peek().a);
		for (String s : list) {
			pw.println(s);
		}
		pw.flush();
	}

	static void calc(Obj o1, Obj o2, List<String> list, FenwickTree ft,
			int n, int i, PriorityQueue<Obj> que) {
		boolean z = false;
		for (Long k : o1.map.keySet()) {
			if (o2.map.containsKey(k)) {
				z = true;
				break;
			}
		}

		StringBuilder sb = new StringBuilder();
		sb.append(o1.i + 1 - ft.sum(o1.i)).append(' ');
		sb.append(o2.i + 1 - ft.sum(o2.i));
		list.add(sb.toString());

		Obj o = new Obj();
		o.i = n + i;
		if (z) {
			o.map = new HashMap<>();
		} else {
			o.a = Math.max(o1.a - 1, 0) * Math.max(o2.a - 1, 0);
			o.map = bunkai(o.a);
		}
		o.b = minKey(o.map);
		que.add(o);
		ft.add(o1.i, 1);
		ft.add(o2.i, 1);
	}

	static class Obj {
		int i;
		long a, b;
		Map<Long, Integer> map;
	}

	static long minKey(Map<Long, Integer> map) {
		if (map.isEmpty()) {
			return 0;
		}
		long ret = Long.MAX_VALUE;
		for (Long k : map.keySet()) {
			ret = Math.min(ret, k);
		}
		return ret;
	}

	static Map<Long, Integer> bunkai(long n) {
		Map<Long, Integer> soinsu = new HashMap<>();
		int end = (int) Math.sqrt(n);
		long d = 2;
		while (n > 1) {
			if (n % d == 0) {
				n /= d;
				soinsu.put(d, soinsu.getOrDefault(d, 0) + 1);
				end = (int) Math.sqrt(n);
			} else {
				if (d > end) {
					d = n - 1;
				}
				d++;
			}
		}
		return soinsu;
	}
}

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