結果
問題 | No.14 最小公倍数ソート |
ユーザー |
|
提出日時 | 2018-02-21 09:13:50 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 50 ms / 5,000 ms |
コード長 | 1,808 bytes |
コンパイル時間 | 2,082 ms |
コンパイル使用メモリ | 177,440 KB |
実行使用メモリ | 8,960 KB |
最終ジャッジ日時 | 2024-09-14 12:28:40 |
合計ジャッジ時間 | 3,511 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 20 |
ソースコード
#define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define MT make_tuple #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)<<endl #define smax(x,y) (x)=max((x),(y)) #define smin(x,y) (x)=min((x),(y)) #define MEM(x,y) memset((x),(y),sizeof (x)) #define sz(x) (int)(x).size() #define RT return #define vv(a,b,c,d) vector<vector<a> >(b,vector<a>(c,d)) #define vvv(a,b,c,d,e) vector<vector<vector<a> > >(b,vv(a,c,d,e)) using ll = long long; using pii = pair<int, int>; using vi = vector<int>; using vll = vector<ll>; vector<int> divisors(int n) { vector<int> res; for (int i = 1; (long long)i*i <= n; ++i) { if (n%i == 0) { res.push_back(i); int j = n / i; if (j != i)res.push_back(j); } } return res; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20); int N; cin >> N; multiset<int> S[10001]; vi ds[10001]; vi a(N), p(N); rep(i, N) { cin >> a[i]; ds[a[i]] = divisors(a[i]); sort(all(ds[a[i]])); each(d, ds[a[i]])S[d].insert(a[i]); } p[0] = a[0]; rep(i, N - 1) { int b = p[i]; each(d, ds[b]) { auto it = S[d].find(b); S[d].erase(it); } int c = INT_MAX, mi = INT_MAX; each(g, ds[b]) if(sz(S[g])){ int x = *S[g].begin(); int l = b * x / g; if (l < mi || (l == mi && x < c)) { mi = l; c = x; } } p[i + 1] = c; } rep(i, N)cout << p[i] << (i != N - 1 ? ' ' : '\n'); }