/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 8ms 1.285 MiB
#2 Wrong Answer 8ms 1.141 MiB
#3 Wrong Answer 8ms 1.184 MiB

Code

#include <bits/stdc++.h>
#ifdef Sabbir
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"
#endif
using namespace std;
#ifdef Sabbir
#include <E:\CodeBox\TempBox\Debug\debug.h>
#else
#define dbg(x...)
#endif

#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
// Ordered set that supports order statistics.
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

#define ll long long
#define all(x) (x).begin(), (x).end()
#define in(v) for(auto &b: v) cin >> b;
#define pb push_back

// Global variables for sieve.
vector<int> primes;
const ll mx = 1e6+1;
vector<bool> isprime(mx, true);

// Sieve to generate primes up to mx.
void sieve(){
    isprime[1] = false;
    for (ll i = 2; i * i <= mx; i++){
        if(isprime[i]){
            for (ll j = i * i; j <= mx; j += i){
                isprime[j] = false;
            }
        }
    }
    for (int i = 2; i < mx; i++){
        if(isprime[i])
            primes.pb(i);
    }
}

void pagol(){
    int n;
    cin >> n;
    vector<int> C(n);
    in(C);
    // We'll group indices by their smallest prime factor (SPF).
    // Since SPF(1) is defined as 1, we use index 1 for that group.
    vector<vector<int>> groups(n + 1);
    
    groups[1].pb(1); // Group for SPF = 1 (only index 1)
    int mot = 0;    // Track maximum prime used among indices 2..n
    for (int i = 2; i <= n; i++){
        for (auto a : primes){
            if(i % a == 0){
                groups[a].pb(i);
                mot = max(mot, a);
                break;
            }
        }
    }
    
    // This will store our final answer (1-indexed).
    vector<int> ans(n + 1, 0);
     ans[1]=1;
    
    // Process groups for each prime (for indices with SPF >= 2).
    for(auto a : primes){
        if(a > mot) break;
        if(groups[a].empty()) continue;
        reverse(all(groups[a]));
        ordered_set os;
        for(auto idx : groups[a]){
            int required = C[idx - 1];
            if(required > os.size()){
                cout << "NO" << "\n";
                return;
            }
            if(required == os.size()){
                if(os.size() > 0)
                    os.insert((*os.begin()) - 1);
                else
                    os.insert(-10000000);
                ans[idx] = -(*os.begin());
            } else {
                ans[idx] = -(*os.find_by_order(required));
            }
        }
    }
    
    cout << "YES" << "\n";
    for (int i = 1; i <= n; i++){
        cout << ans[i] << " ";
    }
    cout << "\n";
}

int32_t main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    sieve();
    int t;
    cin >> t;
    while(t--){
        pagol();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1163 Roy and Array Construction
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-17 17:46:33
Judged At
2025-02-17 17:46:33
Judged By
Score
2
Total Time
8ms
Peak Memory
1.285 MiB