結果

問題 No.14 最小公倍数ソート
ユーザー keikei
提出日時 2018-03-31 00:54:21
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 4,673 ms / 5,000 ms
コード長 3,112 bytes
コンパイル時間 1,706 ms
コンパイル使用メモリ 165,444 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-08 07:53:57
合計ジャッジ時間 53,862 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 50 ms
4,384 KB
testcase_04 AC 4,673 ms
4,380 KB
testcase_05 AC 1,570 ms
4,384 KB
testcase_06 AC 1,834 ms
4,380 KB
testcase_07 AC 2,397 ms
4,384 KB
testcase_08 AC 3,145 ms
4,380 KB
testcase_09 AC 4,263 ms
4,380 KB
testcase_10 AC 4,186 ms
4,380 KB
testcase_11 AC 4,333 ms
4,384 KB
testcase_12 AC 4,462 ms
4,384 KB
testcase_13 AC 4,514 ms
4,380 KB
testcase_14 AC 4,393 ms
4,380 KB
testcase_15 AC 4,534 ms
4,380 KB
testcase_16 AC 1,685 ms
4,380 KB
testcase_17 AC 1,203 ms
4,384 KB
testcase_18 AC 535 ms
4,384 KB
testcase_19 AC 2,842 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:7:17: 警告: overflow in conversion from ‘double’ to ‘ll’ {aka ‘int’} changes value from ‘1.0e+18’ to ‘2147483647’ [-Woverflow]
    7 | const ll LINF = 1e18;
      |                 ^~~~

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
typedef int 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; }

void solve(){
    ll N; scanf("%d",&N); // cin >> N;
    vector<ll> a(N);
    for(int i = 0; i < N;i++) scanf("%d",&a[i]);
    for(int i = 0; i < N;i++){
        if(i!=0) putchar(' ');
        printf("%d",a[i]);
        if(i == N-1) break;
        ll next = i + 1, Max = lcm(a[i],a[i+1]);
        for(int j = i + 2; j < N;j++){
            ll T = lcm(a[i],a[j]);
            if(T < Max){
                next = j; Max = T;
            }else if(T == Max && a[j] < a[next]){
                next = j; Max = T;
            }
        }
        swap(a[i+1],a[next]);
    }
    putchar('\n');
    return;
}
int main(void) {
   //cin.tie(0); ios::sync_with_stdio(false);
    solve();
    return 0;
}
0