48992023-04-06 02:53:11SleepyOverlordKövetkező permutáció (50 pont)cpp17Time limit exceeded 24/50347ms4524 KiB
#include <vector>
#include <string> 
#include <set> 
#include <map> 
#include <unordered_set>
#include <unordered_map>
#include <queue> 
#include <bitset> 
#include <stack>
#include <list>

#include <numeric> 
#include <algorithm> 
#include <random>
#include <chrono>

#include <cstdio>
#include <fstream>
#include <iostream> 
#include <sstream> 
#include <iomanip>
#include <climits>

#include <cctype>
#include <cmath> 
#include <ctime>
#include <cassert>

using namespace std;

#define ULL unsigned long long
#define LL long long
#define PII pair <int, int>
#define VB vector <bool>
#define VI vector <int>
#define VLL vector <LL>
#define VD vector <double>
#define VS vector <string>
#define VPII vector <pair <int, int> >
#define VVI vector < VI >
#define VVB vector < VB >
#define SI set < int >
#define USI unordered_set <int>
#define MII map <int, int>
#define UMII unordered_map <int, int>
#define MS multiset
#define US unordered_set
#define UM unordered_map
#define UMS unordered_multiset
#define UMM unordered_multimap

#define FORN(i, n) for(int i = 0; i < (n); ++i)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)

#define SZ size()
#define BG begin() 
#define EN end() 
#define CL clear()
#define X first
#define Y second
#define RS resize
#define PB push_back
#define MP make_pair
#define ALL(x) x.begin(), x.end()
#define INS insert
#define ER erase
#define CNT count

int n;
VI a, biggestBefore;

bool good(int value)
{
	FOR(j, 1, value - 1)
		if (biggestBefore[j] > value)
		{
			return false;
		}
	return true;
}

int prefMax(int index)
{
	int mx = 0;
	FOR(i, 1, index) mx = max(mx, a[i]);
	return mx;
}

int main()
{
	cin >> n;
	a.RS(n + 1);
	biggestBefore.RS(n + 1);
	FOR(i, 1, n)
	{
		cin >> a[i];
		biggestBefore[a[i]] = prefMax(i - 1);
	}

	SI unused;
	int i = n;
	while (1)
	{
		unused.INS(a[i]);
		biggestBefore[a[i]] = 0;
		auto it = unused.upper_bound(a[i]);
		if (it != unused.EN)
		{
			if (good(*it))
			{
				a[i] = *it;
				biggestBefore[a[i]] = prefMax(i - 1);
				unused.erase(it);
				int firstBad = -1;
				FOR(j, i + 1, n)
				{
					if (good(*unused.BG))
					{
						a[j] = *unused.BG;
						biggestBefore[a[j]] = prefMax(j - 1);
						unused.erase(unused.BG);
					}
					else
					{
						firstBad = j;
						break;
					}
				}
				if (firstBad == -1)
				{
					FOR(i, 1, n) cout << a[i] << " ";
					cout << endl;
					return 0;
				}
				else 
				{
					FOR(j, i + 1, firstBad - 1) 
					{
						unused.INS(a[j]);
						biggestBefore[a[j]] = 0;
					}
					i = firstBad - 1;
				}
			}
			else
			{
				biggestBefore[a[i]] = 0;
				--i;
			}
		}
		else 
		{
			biggestBefore[a[i]] = 0;
			--i;
		}
	}

	cout << -1;
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base24/50
1Accepted0/03ms1812 KiB
2Time limit exceeded0/0300ms2200 KiB
3Accepted1/13ms2384 KiB
4Accepted1/13ms2604 KiB
5Accepted1/13ms2816 KiB
6Accepted1/12ms2924 KiB
7Accepted2/23ms3052 KiB
8Accepted2/23ms3060 KiB
9Accepted2/23ms3404 KiB
10Accepted2/23ms3644 KiB
11Accepted1/13ms3708 KiB
12Accepted1/13ms3716 KiB
13Accepted1/13ms3792 KiB
14Accepted1/13ms3944 KiB
15Accepted3/36ms4076 KiB
16Accepted3/34ms4156 KiB
17Time limit exceeded0/3300ms4128 KiB
18Time limit exceeded0/3266ms4160 KiB
19Time limit exceeded0/3347ms4292 KiB
20Time limit exceeded0/3256ms3424 KiB
21Time limit exceeded0/3272ms4192 KiB
22Time limit exceeded0/3252ms4328 KiB
23Time limit exceeded0/3268ms4504 KiB
24Time limit exceeded0/3256ms4364 KiB
25Time limit exceeded0/2272ms4144 KiB
26Accepted2/254ms4524 KiB