49022023-04-06 13:18:53SleepyOverlordKövetkező permutáció (50 pont)cpp17Time limit exceeded 27/50335ms5028 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, tree, tree2;

int prefMax(int node, int left, int right, int l, int r, VI &t)
{
	if (l > r) return 0;
	if (l <= left && right <= r) return t[node];
	int mid = (left + right) / 2;
	int ans = 0;
	if (l <= mid) ans = prefMax(node * 2, left, mid, l, r, t);
	if (r > mid) ans = max(ans, prefMax(node * 2 + 1, mid + 1, right, l, r, t));
	return ans;
}

void update(int node, int left, int right, int index, int value, VI &t)
{
	if (left == right)
	{
		t[node] = value;
		return;
	}
	int mid = (left + right) / 2;
	if (index <= mid) update(node * 2, left, mid, index, value, t);
	else update(node * 2 + 1, mid + 1, right, index, value, t);
	t[node] = max(t[node * 2], t[node * 2 + 1]);
}

bool good(int value)
{
	return prefMax(1, 1, n, 1, value - 1, tree2) <= value;
}

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

	SI unused;
	int i = n;
	while (1)
	{
		unused.INS(a[i]);
		biggestBefore[a[i]] = 0;
		update(1, 1, n, a[i], biggestBefore[a[i]], tree2);
		auto it = unused.upper_bound(a[i]);
		if (it != unused.EN)
		{
			if (good(*it))
			{
				a[i] = *it;
				update(1, 1, n, i, a[i], tree);
				biggestBefore[a[i]] = prefMax(1, 1, n, 1, i - 1, tree);
				update(1, 1, n, a[i], biggestBefore[a[i]], tree2);
				unused.erase(it);
				int firstBad = -1;
				FOR(j, i + 1, n)
				{
					if (good(*unused.BG))
					{
						a[j] = *unused.BG;
						update(1, 1, n, j, a[j], tree);
						biggestBefore[a[j]] = prefMax(1, 1, n, 1, j - 1, tree);
						update(1, 1, n, a[j], biggestBefore[a[j]], tree2);
						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;
						update(1, 1, n, a[j], biggestBefore[a[j]], tree2);
					}
					i = firstBad - 1;
				}
			}
			else
			{
				biggestBefore[a[i]] = 0;
				update(1, 1, n, a[i], biggestBefore[a[i]], tree2);
				--i;
			}
		}
		else 
		{
			biggestBefore[a[i]] = 0;
			update(1, 1, n, a[i], biggestBefore[a[i]], tree2);
			--i;
		}
	}

	cout << -1;
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base27/50
1Accepted0/03ms1808 KiB
2Time limit exceeded0/0300ms1520 KiB
3Accepted1/13ms2280 KiB
4Accepted1/13ms2428 KiB
5Accepted1/12ms2512 KiB
6Accepted1/13ms2644 KiB
7Accepted2/22ms2724 KiB
8Accepted2/23ms2724 KiB
9Accepted2/23ms3000 KiB
10Accepted2/22ms2808 KiB
11Accepted1/13ms2936 KiB
12Accepted1/13ms3132 KiB
13Accepted1/13ms3224 KiB
14Accepted1/13ms3352 KiB
15Accepted3/37ms3564 KiB
16Accepted3/34ms3708 KiB
17Accepted3/3157ms4084 KiB
18Time limit exceeded0/3280ms4312 KiB
19Time limit exceeded0/3268ms3632 KiB
20Time limit exceeded0/3282ms3728 KiB
21Time limit exceeded0/3275ms3728 KiB
22Time limit exceeded0/3254ms3772 KiB
23Time limit exceeded0/3263ms3804 KiB
24Time limit exceeded0/3335ms3764 KiB
25Time limit exceeded0/2280ms4188 KiB
26Accepted2/210ms5028 KiB