/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Accepted 1ms 580.0 KiB
#3 Accepted 4ms 532.0 KiB
#4 Accepted 28ms 608.0 KiB
#5 Accepted 74ms 1.129 MiB
#6 Accepted 386ms 9.242 MiB
#7 Accepted 533ms 33.672 MiB
#8 Accepted 390ms 33.672 MiB
#9 Accepted 322ms 33.883 MiB
#10 Accepted 517ms 33.883 MiB
#11 Accepted 342ms 14.527 MiB
#12 Accepted 307ms 6.277 MiB
#13 Accepted 98ms 1.637 MiB
#14 Accepted 90ms 1.273 MiB
#15 Accepted 73ms 16.633 MiB
#16 Accepted 311ms 33.676 MiB
#17 Accepted 403ms 33.883 MiB
#18 Accepted 174ms 2.742 MiB
#19 Accepted 191ms 3.215 MiB
#20 Accepted 246ms 5.918 MiB

Code

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define int        long long int
#define pb         push_back
#define all(x)     x.begin(),x.end()
#define allr(x)    x.rbegin(),x.rend()
#define ii         pair<int,int>
#define endl       '\n'

template <class T>
using orderedSet =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

void pipra(int tc) {
    int n;
    cin >> n;
    vector<int> a(n + 1);
    for(int i = 1 ; i <= n ; i++)
        cin >> a[i];

    vector<int> spf(n + 1, 0), primes;
    spf[1] = 1;
    for(int i = 2 ; i <= n ; i++) {
        if(spf[i] == 0) {
            spf[i] = i;
            primes.pb(i);
        }
        for(int j = 0 ; i * primes[j] <= n ; j++) {
            spf[i * primes[j]] = primes[j];
            if(primes[j] == spf[i])
                break;
        }
    }
    map<int, vector<int>> m;
    for(int i = 1 ; i <= n ; i++) {
        m[spf[i]].pb(i);
    }
    for(auto [e, v] : m) {
        orderedSet<int> os;
        for(int i = 1 ; i <= v.size() ; i++) {
            os.insert(i);
        }
        for(auto x : v) {
            auto it = os.find_by_order(a[x]);
            if(it == os.end()) {
                cout << "NO\n";
                return;
            }
            a[x] = *it;
            os.erase(it);
        }
    }
    cout << "YES\n";
    for(int i = 1 ; i <= n ; i++)
        cout << a[i] << ' ';
    cout << endl;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    
    int t = 1;
    cin >> t;
    for(int i = 1 ; i <= t ; i++)
        pipra(i);
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1163 Roy and Array Construction
Language
C++17 (G++ 13.2.0)
Submit At
2025-04-11 20:18:59
Judged At
2025-04-11 20:18:59
Judged By
Score
100
Total Time
533ms
Peak Memory
33.883 MiB