【題解】Codeforces 1401C. Mere Array

【題目敘述】http://codeforces.com/contest/1401/problem/C

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
 
int t, n, a[100005], mn;
vector <int> v;
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> t;
    while (t--){
        cin >> n;
        mn = 1e9;
        for (int i = 1; i <= n; i++){
            cin >> a[i];
            mn = min(mn, a[i]);
        }
        v.clear();
        for (int i = 1; i <= n; i++){
            if (a[i] % mn == 0){
                v.push_back(a[i]);
                a[i] = 0;
            }
        }
        sort(v.begin(), v.end());
        int p = 0;
        bool ans = true;
        for (int i = 1; i <= n; i++){
            if (!a[i]){
                a[i] = v[p];
                p++;
            }
            if (a[i] < a[i-1]) ans = false;
        }
        if (ans) cout << "YES\n";
        else cout << "NO\n";
    }
}
分享本文 Share with friends