252692026-02-18 22:08:23miszorimarciKövetkező permutáció (50 pont)cpp17Accepted 50/506ms1028 KiB
#include <bits/stdc++.h>
using namespace std;

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

    int n;cin >> n;
    vector<int> P(n);
    vector<int> pr(n);
    int mx = 0;

    for (int i = 0; i < n; ++i) {
        cin >> P[i];
        pr[i] = mx;
        if (P[i] > mx) {
            mx = P[i];
        }
    }

    set<int> a;
    for (int i = n - 1; i >= 0; --i) {
        a.insert(P[i]);

        int v = -1; 
        int pm = pr[i];

        auto itA = a.upper_bound(max(P[i], pm));
        if (itA != a.end()) {
            v = *itA;
        }

        auto itL = a.lower_bound(pm);
        if (itL != a.begin()) {
            int L = *(--itL);
            if (L > P[i]) {
                if (v == -1 || L < v) {
                    v = L;
                }
            }
        }

        if (v != -1) {
            P[i] = v;
            a.erase(v);

            int new_max = max(pm, v);
            vector<int> low, high;
            for (int x : a) {
                if (x < new_max) low.push_back(x);
                else high.push_back(x);
            }

            reverse(low.begin(), low.end());
            for (int j = 0; j < i; ++j) cout << P[j] << " ";
            cout << P[i];
            for (int x : low) cout << " " << x;
            for (int x : high) cout << " " << x;
            cout << endl;

            return 0;
        }
    }

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms316 KiB
2Accepted0/02ms316 KiB
3Accepted1/11ms316 KiB
4Accepted1/11ms316 KiB
5Accepted1/11ms316 KiB
6Accepted1/11ms316 KiB
7Accepted2/21ms316 KiB
8Accepted2/21ms316 KiB
9Accepted2/21ms512 KiB
10Accepted2/21ms500 KiB
11Accepted1/11ms316 KiB
12Accepted1/11ms316 KiB
13Accepted1/11ms316 KiB
14Accepted1/11ms316 KiB
15Accepted3/31ms316 KiB
16Accepted3/31ms316 KiB
17Accepted3/31ms316 KiB
18Accepted3/31ms316 KiB
19Accepted3/32ms500 KiB
20Accepted3/32ms316 KiB
21Accepted3/32ms316 KiB
22Accepted3/33ms316 KiB
23Accepted3/33ms316 KiB
24Accepted3/33ms316 KiB
25Accepted2/26ms1028 KiB
26Accepted2/23ms316 KiB