49052023-04-06 16:27:14SleepyOverlordKövetkező permutáció (50 pont)cpp17Time limit exceeded 0/50277ms167304 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

#define IN_FILE "a.in"
#define OUT_FILE "a.out"

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, int index, SI &unused)
{
	bool ok1 = prefMax(1, 1, n, 1, value - 1, tree2) <= value;
	auto it = unused.upper_bound(value);
	bool ok2 = it == unused.EN || *it > prefMax(1, 1, n, 1, index - 1, tree);
	return ok1 && ok2;
}

int main()
{
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);

	cin >> n;
	a.RS(n + 1);
	tree.RS(4 * n + 1); //max(a[i..j])
	tree2.RS(4 * n + 1); //max(biggestBefore[i..j])
	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, i, unused))
			{
				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)
				{
					int aux = prefMax(1, 1, n, 1, j - 1, tree);
					auto it2 = unused.lower_bound(aux);
					if (it2 != unused.BG) --it2;
					bool ok = false;
					while (it2 != unused.EN)
					{
						if (good(*it2, j, unused))
						{
							a[j] = *it2;
							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(it2);
							ok = true;
							break;
						}
						++it2;
					}
					if (!ok)
					{
						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
base0/50
1Time limit exceeded0/0277ms167304 KiB
2Runtime error0/028ms64836 KiB
3Runtime error0/128ms64552 KiB
4Runtime error0/134ms64488 KiB
5Runtime error0/128ms64320 KiB
6Runtime error0/128ms64088 KiB
7Runtime error0/227ms63996 KiB
8Runtime error0/232ms63748 KiB
9Runtime error0/232ms63508 KiB
10Runtime error0/227ms63436 KiB
11Runtime error0/132ms63284 KiB
12Runtime error0/132ms63044 KiB
13Runtime error0/132ms62808 KiB
14Runtime error0/132ms62796 KiB
15Runtime error0/332ms62556 KiB
16Runtime error0/330ms62412 KiB
17Runtime error0/330ms62400 KiB
18Runtime error0/326ms62152 KiB
19Runtime error0/330ms61936 KiB
20Runtime error0/328ms61940 KiB
21Runtime error0/330ms61940 KiB
22Runtime error0/330ms61944 KiB
23Runtime error0/327ms61948 KiB
24Runtime error0/327ms61944 KiB
25Runtime error0/226ms61876 KiB
26Runtime error0/226ms61800 KiB