/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Wrong Answer 25ms 27.27 MiB
#2 Wrong Answer 26ms 27.23 MiB

Code

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;

const int MAX_N = 1000000;
vector<int> spf(MAX_N + 1);

// Sieve function to compute Smallest Prime Factors (SPF)
void sieve() {
    iota(spf.begin(), spf.end(), 0); // Initialize spf with 0, 1, 2, ..., MAX_N
    for (int i = 2; i * i <= MAX_N; ++i) {
        if (spf[i] == i) {
            for (int j = i * i; j <= MAX_N; j += i) {
                if (spf[j] == j) {
                    spf[j] = i;
                }
            }
        }
    }
}

void solve() {
    int t;
    cin >> t;
    sieve(); // Precompute the Smallest Prime Factors

    while (t--) {
        int n;
        cin >> n;
        vector<int> C(n);
        for (int i = 0; i < n; ++i) {
            cin >> C[i];
        }

        vector<int> A(n);
        vector<vector<int>> grouped(MAX_N + 1);

        for (int i = 0; i < n; ++i) {
            int p = spf[i + 1];
            grouped[p].push_back(i);
        }

        bool possible = true;
        for (const auto& group : grouped) {
            if (!group.empty()) {
                int len = group.size();
                vector<int> values(len);
                iota(values.begin(), values.end(), 1); // Fill values with 1, 2, ..., len

                for (int i : group) {
                    if (C[i] >= len) {
                        possible = false;
                        break;
                    }
                }

                if (!possible) break;

                for (int i : group) {
                    A[i] = 1000000000 - values[C[i]];
                    values.erase(values.begin() + C[i]);
                }
            }
        }

        if (possible) {
            cout << "YES\n";
            for (int i = 0; i < n; ++i) {
                cout << A[i] << " ";
            }
            cout << "\n";
        } else {
            cout << "NO\n";
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1163 Roy and Array Construction
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-15 11:13:46
Judged At
2025-02-15 11:13:46
Judged By
Score
0
Total Time
26ms
Peak Memory
27.27 MiB