結果
問題 | No.14 最小公倍数ソート |
ユーザー |
![]() |
提出日時 | 2016-02-10 21:10:05 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 27 ms / 5,000 ms |
コード長 | 1,089 bytes |
コンパイル時間 | 1,461 ms |
コンパイル使用メモリ | 166,616 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-21 23:57:57 |
合計ジャッジ時間 | 3,034 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 20 |
ソースコード
#include <bits/stdc++.h> using namespace std; typedef pair<int,int> P; int a[10010]; int res[10010]; int done[10010]; priority_queue<P,vector<P>,greater<P>> Q[10010]; int main(){ int N; cin >> N; for(int i = 0 ; i < N ; i++){ cin >> a[i]; for(int j = 1 ; j * j <= a[i] ; j++){ if( a[i] % j == 0 ){ Q[a[i]/j].push({a[i],i}); Q[j].push({a[i],i}); } } } int p; for(int i = 0 ; i < N ; i++){ if( i == 0 ){ res[0] = 0; p = a[0]; done[0] = 1; }else{ vector<int> d; for(int j = 1 ; j * j <= p ; j++){ if( p % j == 0 ){ d.push_back(p/j); d.push_back(j); } } array<int,3> cand = {INT_MAX,INT_MAX,INT_MAX}; for( auto e : d ){ while( Q[e].size() && done[Q[e].top().second] ) Q[e].pop(); if( Q[e].size() ){ cand = min(cand,{p / e * Q[e].top().first,Q[e].top().first,Q[e].top().second}); } } p = cand[1]; res[i] = cand[2]; // cout << cand.first << " " << cand.second << "|" << endl; done[cand[2]] = 1; } } for(int i = 0 ; i < N ; i++){ cout << a[res[i]] << (i+1==N?"\n":" "); } }