48752023-04-05 20:31:16CWMKövetkező permutáció (50 pont)cpp17Accepted 50/504ms4904 KiB
#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>
#include <unordered_set>
#include <queue>

//upper bound >
//lower bound >=
//int index = (lower_bound(testvec.begin(), testvec.end(), num)-testvec.begin());

using namespace std;
using ll = long long;
int mod = 1000000007;

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int n;
    cin >> n;
    vector<int> startingList;
    for (size_t i = 0; i < n; i++)
    {
        int a;
        cin >> a;
        startingList.push_back(a);
    }
    for (size_t i = 0; i < n - 1; i++)
    {
        if (startingList[n - i - 1] > startingList[n - i - 2]) {
            vector<int> ToSort;
            int begin = n - i - 2;
            for (size_t j = begin; j < n; j++)
            {
                ToSort.push_back(startingList[j]);
            }
            sort(ToSort.begin(), ToSort.end());
            int index = (upper_bound(ToSort.begin(), ToSort.end(), startingList[begin]) - ToSort.begin());
            vector<int> alrSorted;
            for (size_t j = 0; j < ToSort.size(); j++)
            {
                if (j != index) {
                    alrSorted.push_back(ToSort[j]);
                }
            }
            startingList[begin] = ToSort[index];
            for (size_t j = 0; j < alrSorted.size(); j++)
            {
                startingList[begin + 1 + j] = alrSorted[j];
            }
            break;
        }
    }
    // Found next iteration, is it correct though?
    int max = -1;
    int secMax = -1;
    int issueIndex = -1;
    vector<int> ToSort;
    for (int i = startingList.size() - 1; i >= 0; i--)
    {
        if (startingList[i] > max) {
            if (secMax != -1) {
                issueIndex = i;
                break;
            }
            else {
                max = startingList[i];
            }
        }
        else if (secMax == -1) {
            secMax = startingList[i];
        }
        else if (startingList[i]<secMax) {
            max = secMax;
            secMax = startingList[i];
        }
    }
    if (issueIndex == -1) {
        for (size_t i = 0; i < n; i++)
        {
            cout << startingList[i] << " ";
        }
        return 0;
    }
    for (size_t j = issueIndex + 1; j < n; j++)
    {
        ToSort.push_back(startingList[j]);
    }
    sort(ToSort.begin(), ToSort.end());
    int largerThanIssueIndex = (upper_bound(ToSort.begin(), ToSort.end(), startingList[issueIndex]) - ToSort.begin());
    vector<int> AlrSorted;
    for (size_t i = 0; i < largerThanIssueIndex; i++)
    {
        AlrSorted.push_back(ToSort[largerThanIssueIndex - 1 - i]);
    }
    for (size_t i = largerThanIssueIndex; i < ToSort.size(); i++)
    {
        AlrSorted.push_back(ToSort[i]);
    }
    for (size_t i = issueIndex + 1; i < n; i++)
    {
        startingList[i] = AlrSorted[i - issueIndex - 1];
    }
    for (size_t i = 0; i < n; i++)
    {
        cout << startingList[i] << " ";
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1828 KiB
2Accepted0/04ms2216 KiB
3Accepted1/13ms2232 KiB
4Accepted1/13ms2444 KiB
5Accepted1/13ms2656 KiB
6Accepted1/13ms2756 KiB
7Accepted2/23ms2968 KiB
8Accepted2/23ms3064 KiB
9Accepted2/22ms3048 KiB
10Accepted2/22ms3048 KiB
11Accepted1/13ms3288 KiB
12Accepted1/12ms3268 KiB
13Accepted1/13ms3392 KiB
14Accepted1/12ms3476 KiB
15Accepted3/33ms3752 KiB
16Accepted3/33ms3872 KiB
17Accepted3/33ms4092 KiB
18Accepted3/33ms4144 KiB
19Accepted3/34ms4188 KiB
20Accepted3/34ms4456 KiB
21Accepted3/34ms4576 KiB
22Accepted3/34ms4664 KiB
23Accepted3/34ms4884 KiB
24Accepted3/34ms4868 KiB
25Accepted2/24ms4904 KiB
26Accepted2/24ms4788 KiB