209242026-01-11 14:07:01ubormaciTitkos sorozatcpp17Accepted 40/4090ms13512 KiB
#include <iostream>
#include <algorithm> // for sort, mainly
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <array>
#include <string>
#include <cstdio>
#include <iterator>
#include <unordered_set>
#include <cstdint> // for int64_t, int32_t, etc
#include <queue>
#include <stack>
#include <deque>
#include <numeric> // gcd, lcm
#include <fstream>
#include <bitset> // for bitset
#include <iomanip>
#include <cassert> // for set with custom ordering
#include <type_traits> // for set with custom ordering
#include <utility> // for swap, forward, etc
using namespace std;

#pragma GCC optimize("O2")
// #pragma GCC optimize("O1","O2","O3","Ofast","unroll-loops")
//#pragma GCC target("sse","sse2","sse3","sse4.1","sse4.2","avx","avx2","fma")

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

/*
 *
 * notes:
 *
 * int64_t
 * stoi(string s) -> string to int
 * to_string() -> int (or else) to string
 *
 * vector declaration:
 * vector<ll> v(n, 0)
 * vector<vector<ll>> v(n, vector<ll>(n, 0));
 *
 * {if statement} ? {truth value} : {false value}
 *
 * set lower bound/upper bound:
 * 	// . . . m1 . . . d . . . . m2
 *    auto m1_it = b.lower_bound(d);
 *    advance(m1_it, -1);
 *    m1 = *m1_it;
 *	m2 = *b.upper_bound(d);
 *
 * #ifdef LOCAL
 *    freopen("in.txt","r",stdin);
 *    freopen("out.txt","w",stdout);
 * #endif
 *
 * constexpr auto lcm(auto x, auto... xs)
 * {
 *	return ((x = std::lcm(x, xs)), ...);
 * }
 *
 * std::gcd(int a, int b)
 *
 * cout << setprecision(n);
 *
 * set with custom ordering
 * set<ll, decltype(&cmp)> qu(cmp);
 *
 */

typedef int64_t ll;

bool debug = false;

void createtest(ll n) {

    vector<ll> v;
    for(ll i = 1; i <= n; i++) {
        v.push_back(i);
    }

    vector<string> inp;
    vector<string> out;

    do{

        string ans = "";
        string clue = "";

        vector<ll> b(n, 0);
        for(ll i = 0; i < n; i++) {

            // minden elemre megnezzuk, melyik a legkozelebbi, ami nagyobb

            ll ind = -1;
            for(ll j = i+1; j < n; j++) {
                if(v[j] > v[i]) {
                    ind = j;
                    break;
                }
            }

            if(ind == -1) {
                b[i] = ind;
            }else{
                b[i] = ind+1;
            }

        }

        for(const auto & x : b) {
            clue += to_string(x) + " ";
        }
        for(const auto & x : v) {
            ans += to_string(x) + " ";
        }

        inp.push_back(clue);
        out.push_back(ans);

    }while(std::next_permutation(v.begin(), v.end()));

    cout << inp.size() << "\n";
    for(const auto & s : inp) {
        cout << n << "\n" << s << "\n";
    }

    cout << "\nANSWERS\n";

    for(const auto & s : out) {
        cout << s << "\n";
    }

}

void solve() {

    ll n;
    cin >> n;

    vector<ll> b(n+1, 0);
    vector<ll> a(n+1, 0);

    set<ll> s;
    for(ll i = 1; i <= n; i++) {
        s.insert(i);
    }

    for(ll i = 1; i <= n; i++) {
        cin >> b[i];
        if(b[i] == -1) {
            a[i] = *s.rbegin();
            s.erase(a[i]);
        }
    }

    // parent, distance from parent
    set<pair<ll,ll>> rem;

    for(ll i = 1; i <= n; i++) {
        if(b[i] != -1) {
            rem.insert({b[i], b[i] - i});
        }
    }

    //vector<ll> parcnt(n+1, 0);

    while(!rem.empty()) {
        auto f = *rem.rbegin();
        rem.erase(prev(rem.end()));

        ll par = f.first;
        ll dis = f.second;

        //cerr << "\npar=" << par << "; dis=" << dis;

        ll ind = par - dis;

        a[ind] = *s.rbegin();
        s.erase(a[ind]);
    }

    for(ll i = 1; i <= n; i++) {
        cout << a[i] << " ";
    }
    
    if(debug) {
        cout << "\n";
    }
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    //cin.tie(nullptr);
    //cout.tie(nullptr);

    //createtest(4);
    //return 0;

    if(debug) {

        ll test;
        cin >> test;
        for(ll ti = 1; ti <= test; ti++) {
            solve();
        }

    }else{
        solve();
    }

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/01ms316 KiB
2Accepted0/041ms6964 KiB
3Accepted1/11ms500 KiB
4Accepted1/11ms500 KiB
5Accepted2/21ms316 KiB
6Accepted1/12ms508 KiB
7Accepted1/11ms316 KiB
8Accepted2/21ms480 KiB
9Accepted2/290ms13512 KiB
10Accepted2/285ms13368 KiB
11Accepted2/290ms13300 KiB
12Accepted2/289ms13352 KiB
13Accepted2/285ms13412 KiB
14Accepted2/283ms13436 KiB
15Accepted2/289ms13380 KiB
16Accepted3/386ms13364 KiB
17Accepted3/382ms13360 KiB
18Accepted3/383ms13364 KiB
19Accepted3/387ms13364 KiB
20Accepted3/385ms13384 KiB
21Accepted1/182ms11316 KiB
22Accepted1/178ms11072 KiB
23Accepted1/175ms10312 KiB