結果

問題 No.14 最小公倍数ソート
ユーザー koprickykopricky
提出日時 2017-08-20 17:28:01
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,157 bytes
コンパイル時間 1,403 ms
コンパイル使用メモリ 162,472 KB
実行使用メモリ 9,108 KB
最終ジャッジ日時 2023-08-04 20:09:56
合計ジャッジ時間 2,907 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:91:19: warning: ‘idx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
         v.pb(vec[i]);
                   ^

ソースコード

diff #

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(a, b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int>P;
typedef pair<ll,ll>pll;

const int MAX_N = 10005;

set<P> st[MAX_N];
vector<vector<int> > res;

vector<int> divisor(int n)
{
    vector<int> res;
    for(int i=1;i*i<=n;i++){
        if(n%i==0){
            res.pb(i);
            if(i != n/i){
                res.pb(n/i);
            }
        }
    }
    sort(all(res));
    return res;
}

int gcd(int a,int b)
{
    if(a % b == 0){
        return b;
    }else{
        return gcd(b,a%b);
    }
}

int lcm(int a,int b)
{
    return  a / gcd(a,b) * b;
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> vec(n);
    rep(i,n){
        cin >> vec[i];
        res.pb(divisor(vec[i]));
        rep(j,res[i].size()){
            st[res[i][j]].insert(P(vec[i],i));
        }
    }
    vector<int> v;
    v.pb(vec[0]);
    int i = 0;
    rep(id,n-1){
        P ans = P(INF,INF);
        int idx;
        rep(j,res[i].size()){
            st[res[i][j]].erase(P(vec[i],i));
            auto it = st[res[i][j]].begin();
            if(it != st[res[i][j]].end()){
                P p = *it;
                ll hoge = lcm(vec[i],p.fi);
                if(ans > P(hoge,vec[i])){
                    ans = P(hoge,vec[i]);
                    idx = p.se;
                }
            }
        }
        i = idx;
        v.pb(vec[i]);
    }
    rep(i,n){
        cout << v[i] << " ";
    }
    cout << endl;
    return 0;
}
0