結果
問題 | No.14 最小公倍数ソート |
ユーザー |
![]() |
提出日時 | 2018-01-05 01:35:48 |
言語 | Java (openjdk 23) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 2,384 bytes |
コンパイル時間 | 3,091 ms |
コンパイル使用メモリ | 78,664 KB |
実行使用メモリ | 54,868 KB |
最終ジャッジ日時 | 2024-12-23 04:01:29 |
合計ジャッジ時間 | 61,912 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 17 TLE * 3 |
ソースコード
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; } } }