結果
問題 | No.14 最小公倍数ソート |
ユーザー | Grenache |
提出日時 | 2016-06-11 14:52:03 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 521 ms / 5,000 ms |
コード長 | 2,907 bytes |
コンパイル時間 | 4,162 ms |
コンパイル使用メモリ | 80,416 KB |
実行使用メモリ | 51,720 KB |
最終ジャッジ日時 | 2024-04-17 18:46:09 |
合計ジャッジ時間 | 11,426 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 46 ms
37,244 KB |
testcase_01 | AC | 48 ms
37,288 KB |
testcase_02 | AC | 47 ms
37,296 KB |
testcase_03 | AC | 233 ms
49,192 KB |
testcase_04 | AC | 443 ms
50,200 KB |
testcase_05 | AC | 380 ms
50,908 KB |
testcase_06 | AC | 325 ms
49,796 KB |
testcase_07 | AC | 372 ms
50,620 KB |
testcase_08 | AC | 386 ms
50,468 KB |
testcase_09 | AC | 420 ms
50,712 KB |
testcase_10 | AC | 387 ms
50,616 KB |
testcase_11 | AC | 498 ms
50,828 KB |
testcase_12 | AC | 486 ms
51,288 KB |
testcase_13 | AC | 499 ms
50,732 KB |
testcase_14 | AC | 499 ms
51,720 KB |
testcase_15 | AC | 521 ms
50,256 KB |
testcase_16 | AC | 454 ms
49,628 KB |
testcase_17 | AC | 453 ms
50,416 KB |
testcase_18 | AC | 338 ms
49,984 KB |
testcase_19 | AC | 383 ms
50,844 KB |
ソースコード
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); } } }