95082024-02-22 13:04:35Vkrisztian01Következő permutáció (50 pont)cpp17Accepted 50/508ms5840 KiB
#include <iostream>
#include<vector>
#include<set>
#include<algorithm>

using namespace std;

int n,k,maximum=0;
vector<bool>volte;
vector<int>perm;
set<int>szabad;

int main()
{
    cin>>n;
    volte.assign(n+1,true);
    perm.resize(n+1);
    for(int i=1;i<=n;i++) cin>>perm[i];
    for(int i=n;i>=1;i--)
    {
        volte[perm[i]]=false;
        szabad.insert(perm[i]);
        if(szabad.upper_bound(perm[i])==szabad.end()) continue;
        k=*szabad.upper_bound(perm[i]);
        perm[i]=k;
        szabad.erase(k);
        volte[k]=true;
        for(int j=1;j<=i;j++)
        {
            cout<<perm[j]<<" ";
            maximum=max(maximum,perm[j]);
        }
        for(int j=maximum-1;j>=1;j--) if(!volte[j]) cout<<j<<" ";
        for(int j=maximum+1;j<=n;j++) if(!volte[j]) cout<<j<<" ";
        break;
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms2148 KiB
2Accepted0/04ms2364 KiB
3Accepted1/13ms2452 KiB
4Accepted1/13ms2672 KiB
5Accepted1/13ms2776 KiB
6Accepted1/13ms2912 KiB
7Accepted2/23ms3256 KiB
8Accepted2/23ms3228 KiB
9Accepted2/23ms3452 KiB
10Accepted2/23ms3568 KiB
11Accepted1/13ms3568 KiB
12Accepted1/13ms3664 KiB
13Accepted1/13ms3880 KiB
14Accepted1/13ms4136 KiB
15Accepted3/33ms4312 KiB
16Accepted3/33ms4320 KiB
17Accepted3/33ms4456 KiB
18Accepted3/33ms4448 KiB
19Accepted3/34ms4376 KiB
20Accepted3/36ms4668 KiB
21Accepted3/36ms4668 KiB
22Accepted3/37ms4720 KiB
23Accepted3/37ms4768 KiB
24Accepted3/37ms4808 KiB
25Accepted2/28ms5840 KiB
26Accepted2/27ms4904 KiB