結果

問題 No.14 最小公倍数ソート
ユーザー GrenacheGrenache
提出日時 2016-06-11 14:52:03
言語 Java19
(openjdk 21)
結果
AC  
実行時間 583 ms / 5,000 ms
コード長 2,907 bytes
コンパイル時間 3,888 ms
コンパイル使用メモリ 76,560 KB
実行使用メモリ 63,228 KB
最終ジャッジ日時 2023-07-30 17:41:15
合計ジャッジ時間 13,793 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
49,856 KB
testcase_01 AC 46 ms
49,928 KB
testcase_02 AC 47 ms
49,920 KB
testcase_03 AC 274 ms
60,684 KB
testcase_04 AC 515 ms
62,412 KB
testcase_05 AC 489 ms
62,592 KB
testcase_06 AC 443 ms
62,664 KB
testcase_07 AC 520 ms
62,732 KB
testcase_08 AC 431 ms
62,048 KB
testcase_09 AC 518 ms
61,448 KB
testcase_10 AC 465 ms
62,092 KB
testcase_11 AC 577 ms
63,228 KB
testcase_12 AC 539 ms
62,184 KB
testcase_13 AC 512 ms
62,664 KB
testcase_14 AC 583 ms
62,840 KB
testcase_15 AC 472 ms
61,844 KB
testcase_16 AC 436 ms
61,556 KB
testcase_17 AC 418 ms
62,332 KB
testcase_18 AC 372 ms
62,208 KB
testcase_19 AC 479 ms
62,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


public class Main_yukicoder14 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Printer pr = new Printer(System.out);

        int n = sc.nextInt();

        int[] cnt = new int[10000 + 1];
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        List<Integer> ret = new ArrayList<>();
        for (int i = 0; i < n; i++) {
        	int a = sc.nextInt();
        	if (i > 0) {
        		cnt[a]++;
        		pq.add(a);
        	} else {
        		ret.add(a);
        	}
        }

        out:
        while (!pq.isEmpty()) {
        	int e = pq.peek();
        	if (cnt[e] <= 0) {
        		pq.remove();
        		continue;
        	}

            int prev = ret.get(ret.size() - 1);

            SortedSet<Integer> ts = new TreeSet<>();
            for (int i = 1; i * i <= prev; i++) {
            	if (prev % i == 0) {
            		ts.add(i);
            		ts.add(prev / i);
            	}
            }

        	int lcm = lcm(prev, e);
            for (int j = 1; prev * j <= lcm; j++) {
            	for (int d : ts) {
            		if (d * j > 10000) {
            			break;
            		}
            		if (cnt[d * j] > 0 && lcm(prev, d * j) == prev * j) {
            			ret.add(d * j);
            			cnt[d * j]--;
            			continue out;
            		}
            	}
            }
        }

//        pr.println(ret);
//        pr.flush();

        for (int i = 0; i < n; i++) {
        	pr.print(ret.get(i));
        	if (i < n - 1) {
        		pr.print(" ");
        	} else {
        		pr.println();
        	}
        }

        pr.close();
        sc.close();
    }

	private static int lcm(int n, int m) {
		return n / gcd(n, m) * m;
	}

	private static int gcd(int n, int m) {
		if (m == 0) {
			return n;
		} else {
			return gcd(m, n % m);
		}
	}

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0