結果

問題 No.14 最小公倍数ソート
ユーザー mastersatoshimastersatoshi
提出日時 2015-08-06 19:21:05
言語 Java19
(openjdk 21)
結果
AC  
実行時間 965 ms / 5,000 ms
コード長 3,290 bytes
コンパイル時間 3,539 ms
コンパイル使用メモリ 78,220 KB
実行使用メモリ 57,788 KB
最終ジャッジ日時 2023-09-25 04:14:15
合計ジャッジ時間 17,367 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
39,980 KB
testcase_01 AC 121 ms
39,636 KB
testcase_02 AC 123 ms
39,504 KB
testcase_03 AC 323 ms
45,832 KB
testcase_04 AC 965 ms
57,788 KB
testcase_05 AC 619 ms
51,816 KB
testcase_06 AC 669 ms
51,836 KB
testcase_07 AC 704 ms
53,532 KB
testcase_08 AC 768 ms
55,360 KB
testcase_09 AC 952 ms
56,184 KB
testcase_10 AC 876 ms
55,980 KB
testcase_11 AC 928 ms
56,468 KB
testcase_12 AC 944 ms
56,756 KB
testcase_13 AC 941 ms
57,516 KB
testcase_14 AC 942 ms
56,028 KB
testcase_15 AC 948 ms
56,856 KB
testcase_16 AC 693 ms
52,948 KB
testcase_17 AC 601 ms
51,396 KB
testcase_18 AC 517 ms
50,920 KB
testcase_19 AC 804 ms
55,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        Scanner sc = new Scanner(System.in);

        HashMap<Integer, ArrayList<Integer>> divisors = new HashMap();

        HashMap<Integer, TreeMap<Integer, Integer>> target = new HashMap();

        int n = sc.nextInt();

        int[] a = new int[n];
        ArrayList<Integer> out = new ArrayList();

        //1個目は固定
        out.add(sc.nextInt());

        for (int i = 1; i < n; i++) {
            int tmp = sc.nextInt();

            ArrayList<Integer> tmpPrimes = getPrime(tmp);

            divisors.put(tmp, tmpPrimes);

            for (int prime : tmpPrimes) {
                TreeMap<Integer, Integer> tmpTarget;
                if (target.containsKey(prime)) {
                    tmpTarget = target.get(prime);
                } else {
                    tmpTarget = new TreeMap();
                    target.put(prime, tmpTarget);
                }

                if (tmpTarget.containsKey(tmp)) {
                    int count = tmpTarget.get(tmp) + 1;
                    tmpTarget.replace(tmp, count);
                } else {
                    tmpTarget.put(tmp, 1);
                }
            }
        }

        ArrayList<Integer> exe = getPrime(out.get(0));

        for (int i = 1; i < n; i++) {
            int min = Integer.MAX_VALUE;
            int lcm = Integer.MAX_VALUE;

            for (int tmpNum : exe) {
                TreeMap<Integer, Integer> tmpMap = target.get(tmpNum);

                if (tmpMap == null || tmpMap.isEmpty()) {
                    continue;
                }

                int tmpMin = tmpMap.keySet().iterator().next();
                int tmpLcm = getLcm(tmpMin, out.get(i - 1));
                if (lcm > tmpLcm || (lcm == tmpLcm && min > tmpMin)) {
                    lcm = tmpLcm;
                    min = tmpMin;
                }
            }

            exe = divisors.get(min);

            for (int j : exe) {
                TreeMap<Integer, Integer> tmpTree = target.get(j);

                int count = tmpTree.get(min);

                if (count == 1) {
                    tmpTree.remove(min);
                } else {
                    count--;
                    tmpTree.replace(min, count);
                }
            }

            out.add(min);
        }

        System.out.print(out.get(0));

        for (int i = 1; i < out.size(); i++) {
            System.out.print(" " + out.get(i));
        }
        System.out.println();

    }

    private static ArrayList<Integer> getPrime(int source) {
        ArrayList<Integer> ret = new ArrayList();

        int max = source / 2;
        for (int i = 1; i <= max; i++) {
            if (source % i == 0) {
                ret.add(i);
            }
        }

        ret.add(source);
        return ret;
    }

    private static int getLcm(int a, int b) {
        int tmpA = a;
        int tmpB = b;

        while (tmpA != tmpB) {
            if (tmpA > tmpB) {
                tmpA = tmpA - tmpB;
            } else {
                tmpB = tmpB - tmpA;
            }
        }

        return a * b / tmpA;
    }
}
0