結果

問題 No.14 最小公倍数ソート
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2018-01-05 01:35:48
言語 Java19
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,384 bytes
コンパイル時間 3,866 ms
コンパイル使用メモリ 75,304 KB
実行使用メモリ 55,488 KB
最終ジャッジ日時 2023-08-24 19:10:39
合計ジャッジ時間 59,319 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
52,936 KB
testcase_01 AC 83 ms
52,984 KB
testcase_02 AC 79 ms
53,536 KB
testcase_03 AC 181 ms
54,168 KB
testcase_04 TLE -
testcase_05 AC 1,816 ms
54,712 KB
testcase_06 AC 2,079 ms
54,916 KB
testcase_07 AC 2,652 ms
54,364 KB
testcase_08 AC 3,447 ms
54,660 KB
testcase_09 AC 4,583 ms
54,856 KB
testcase_10 AC 4,533 ms
55,488 KB
testcase_11 AC 4,677 ms
54,508 KB
testcase_12 AC 4,793 ms
54,928 KB
testcase_13 AC 4,864 ms
54,620 KB
testcase_14 AC 4,749 ms
54,884 KB
testcase_15 AC 4,866 ms
54,864 KB
testcase_16 AC 1,922 ms
54,980 KB
testcase_17 AC 1,403 ms
54,728 KB
testcase_18 AC 696 ms
53,840 KB
testcase_19 AC 3,128 ms
54,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


class Main {
    static int gcd(int x,int y){
        while(y!=0){
            int r=x%y;
            x=y;
            y=r;
        }
        return x;
    }
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        int n=sc.nextInt();
        int[]a=sc.nextIntArray(n);
        for(int i=0;i<n-1;++i){
            long min=Long.MAX_VALUE;
            int minind=-1;
            for(int j=i+1;j<n;++j){
                int lcm=a[i]/gcd(a[i],a[j])*a[j];
                long v=(long)lcm<<20|a[j];
                if(min>v){
                    min=v;
                    minind=j;
                }
            }
            if(minind!=i+1){
                int t=a[i+1];
                a[i+1]=a[minind];
                a[minind]=t;
            }
        }
        for(int i=0;i<n;++i)
            out.print(a[i]+(i==n-1?"\n":" "));
        out.close();
    }
    // http://codeforces.com/blog/entry/7018
    //-----------PrintWriter for faster output---------------------------------
    public static PrintWriter out;
    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;
        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
        int[] nextIntArray(int n){
            int[]r=new int[n];
            for(int i=0;i<n;++i)r[i]=nextInt();
            return r;
        }
    }
}
0