結果

問題 No.14 最小公倍数ソート
ユーザー keikei
提出日時 2018-03-30 23:56:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,918 bytes
コンパイル時間 1,554 ms
コンパイル使用メモリ 172,880 KB
実行使用メモリ 8,124 KB
最終ジャッジ日時 2023-09-08 07:42:09
合計ジャッジ時間 8,153 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 114 ms
4,384 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:https://yukicoder.me/problems/no/14>
 問題文============================================================
 
 最小公倍数を習ったばかりのLarryは、最小公倍数ソートというのを思いついた。
 
 ここで「最小公倍数ソート」とは、
 N個の整数(重複を含む)が与えられ、それぞれai (1≤i≤N)とする。
 a1を固定し、a2〜aNをそれぞれa1に対して最小公倍数を取り、最小公倍数が小さい順に並べ変えるソートのことであるとする。
 (最小公倍数の最小が複数ある場合は、元の数が少ない方が優先される)
 
 Larryは、
 この時出来た配列を新たにa1…aNの名前をつけなおして操作を続ける。
 次にa2を固定し(a1も固定したまま)、a3〜aNを「最小公倍数ソート」していく。
 次にa3を固定し...と続けていった時にaNまで行った時になる数列を求めてください。
 
 (C/C++だと愚直な解法でぎりぎり通ってしまいますが、計算量が抑えられる方法があるので★4になってます。)
 
 =================================================================
 解説=============================================================
 ================================================================
 */

/*
 gcd : 最大公約数
 lcm : 最小公倍数
 */
inline ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b)*b; }

vector<ll> solve(){
    ll N; cin >> N;
    vector<ll> a(N);
    for(auto& in:a) cin >> in;
    priority_queue<pll,vector<pll>,greater<pll>> pq;
    for(int i = 0; i < N;i++){
        for(int j = i+1; j < N;j++){ pq.push({lcm(a[j],a[i]),a[j]}); }
        for(int j = i+1; j < N;j++){
            auto p = pq.top(); pq.pop();
            a[j] = p.second;
        }
    }
    return a;
}
int main(void) {
    cin.tie(0); ios::sync_with_stdio(false);
    cout << solve() << endl;
    return 0;
}
0