結果

問題 No.14 最小公倍数ソート
ユーザー krotonkroton
提出日時 2014-10-31 11:31:30
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 49 ms / 5,000 ms
コード長 1,432 bytes
コンパイル時間 547 ms
コンパイル使用メモリ 71,468 KB
実行使用メモリ 9,280 KB
最終ジャッジ日時 2023-08-28 23:46:56
合計ジャッジ時間 2,628 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 5 ms
4,552 KB
testcase_04 AC 49 ms
9,280 KB
testcase_05 AC 30 ms
6,968 KB
testcase_06 AC 27 ms
7,240 KB
testcase_07 AC 32 ms
7,656 KB
testcase_08 AC 39 ms
8,316 KB
testcase_09 AC 45 ms
8,916 KB
testcase_10 AC 46 ms
8,756 KB
testcase_11 AC 47 ms
8,992 KB
testcase_12 AC 45 ms
8,936 KB
testcase_13 AC 46 ms
9,028 KB
testcase_14 AC 45 ms
9,088 KB
testcase_15 AC 47 ms
9,164 KB
testcase_16 AC 27 ms
7,288 KB
testcase_17 AC 21 ms
6,588 KB
testcase_18 AC 14 ms
5,752 KB
testcase_19 AC 37 ms
8,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <set>
using namespace std;

vector<int> divisors[10010];
void getDivisors(int n, vector<int>& store){
    for(int d=1;d*d<=n;d++)if(n % d == 0){
        store.push_back(d);
        if(d != n / d){
            store.push_back(n / d);
        }
    }
}

set<pair<int, int>> div_grp[10010];
int a[10010];

int main(){
    int N;
    cin >> N;

    for(int i=0;i<N;i++){
        cin >> a[i];
        getDivisors(a[i], divisors[i]);
    
        for(int d : divisors[i]){
            div_grp[d].insert(make_pair(a[i], i));
        }
    }
    
    cout << a[0];
    
    int prev_id = 0;
    for(int t=1;t<N;t++){
        int mini_v = 1 << 30;
        int mini_k = -1;
        
        auto prev = make_pair(a[prev_id], prev_id);
        for(int d : divisors[prev_id]){
            div_grp[d].erase(prev);
        
            if(!div_grp[d].empty()){
                auto now = *div_grp[d].begin();
                int now_v = now.first / d;
                
                if(now_v < mini_v){
                    mini_v = now_v;
                    mini_k = now.second;
                } else if(now_v == mini_v && now.first < a[mini_k]){
                    mini_k = now.second;
                }
            }
        }
        
        
        cout << " " << a[mini_k];
        prev_id = mini_k;
    }
    
    return 0;
}
0