/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 5ms 2.574 MiB
#2 Accepted 5ms 2.527 MiB
#3 Accepted 5ms 2.277 MiB
#4 Accepted 17ms 2.824 MiB
#5 Accepted 76ms 3.023 MiB
#6 Accepted 155ms 9.117 MiB
#7 Accepted 170ms 24.812 MiB
#8 Accepted 147ms 24.703 MiB
#9 Accepted 73ms 21.453 MiB
#10 Accepted 172ms 24.715 MiB
#11 Accepted 99ms 11.836 MiB
#12 Accepted 147ms 6.867 MiB
#13 Accepted 64ms 3.574 MiB
#14 Accepted 65ms 3.035 MiB
#15 Accepted 112ms 23.883 MiB
#16 Accepted 82ms 21.633 MiB
#17 Accepted 67ms 21.23 MiB
#18 Accepted 117ms 4.754 MiB
#19 Accepted 118ms 4.805 MiB
#20 Accepted 136ms 6.945 MiB

Code

#include <bits/stdc++.h>
using namespace std;
using int64 = long long;

// Fenwick (BIT) for prefix sums + finding k-th “1”
struct Fenwick {
    int n;
    vector<int> f;
    Fenwick(int _n): n(_n), f(n+1, 0) {}
    void update(int i, int v){
        for(; i<=n; i += i&-i) f[i] += v;
    }
    int query(int i) const {
        int s = 0;
        for(; i>0; i -= i&-i) s += f[i];
        return s;
    }
    // find smallest pos such that prefix sum >= k (1 ≤ k ≤ total)
    int find_kth(int k) const {
        int pos = 0;
        int bit = 1 << (31 - __builtin_clz(n));
        for(; bit; bit >>= 1) {
            int nxt = pos + bit;
            if(nxt <= n && f[nxt] < k){
                k -= f[nxt];
                pos = nxt;
            }
        }
        return pos + 1;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    // Precompute SPF up to max N = 5e5
    const int MAXN = 500000;
    vector<int> spf(MAXN+1);
    for(int i=1;i<=MAXN;i++) spf[i] = i;
    for(int p=2; p*p<=MAXN; p++){
        if(spf[p]==p){
            for(int q=p*p; q<=MAXN; q+=p){
                if(spf[q]==q) spf[q] = p;
            }
        }
    }
    spf[1] = 1;  // define SPF(1)=1

    int T; 
    cin >> T;
    while(T--){
        int n;
        cin >> n;
        vector<int> C(n+1);
        for(int i=1;i<=n;i++) cin >> C[i];

        // Group positions by SPF(i)
        unordered_map<int, vector<int>> groups;
        groups.reserve(n*2);
        for(int i=1;i<=n;i++){
            groups[spf[i]].push_back(i);
        }

        vector<int64> A(n+1, 0);
        bool ok = true;

        // Process each group independently
        for(auto &kv : groups){
            auto &pos = kv.second;
            int m = pos.size();

            // Build the inversion-vector c[k] for k=0..m-1
            vector<int> inv(m);
            for(int k=0;k<m;k++){
                int idx = pos[k];
                inv[k] = C[idx];
                // must satisfy 0 ≤ inv[k] ≤ (m-k-1)
                if(inv[k] < 0 || inv[k] > m-k-1){
                    ok = false;
                    break;
                }
            }
            if(!ok) break;

            // Fenwick over 1..m initially all 1's
            Fenwick fw(m);
            for(int i=1;i<=m;i++) fw.update(i,1);

            // Reconstruct values 1..m per group
            // For each k, pick the (inv[k]+1)-th smallest remaining
            for(int k=0;k<m;k++){
                int pick = fw.find_kth(inv[k] + 1);
                // assign that as A at position pos[k]
                A[ pos[k] ] = pick;
                // remove from Fenwick
                fw.update(pick, -1);
            }
        }

        if(!ok){
            cout << "NO\n";
        } else {
            cout << "YES\n";
            // values are between 1..m ≤ n ≤ 5e5; all ≤ 1e9
            // If you prefer to stretch them, you can scale/mix, but not necessary
            for(int i=1;i<=n;i++){
                cout << A[i] << (i<n?' ':'\n');
            }
        }
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1163 Roy and Array Construction
Language
C++17 (G++ 13.2.0)
Submit At
2025-05-13 18:04:43
Judged At
2025-05-13 18:04:43
Judged By
Score
100
Total Time
172ms
Peak Memory
24.812 MiB