49052023-04-06 16:27:14SleepyOverlordKövetkező permutáció (50 pont)cpp17Időlimit túllépés 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base0/50
1Időlimit túllépés0/0277ms167304 KiB
2Futási hiba0/028ms64836 KiB
3Futási hiba0/128ms64552 KiB
4Futási hiba0/134ms64488 KiB
5Futási hiba0/128ms64320 KiB
6Futási hiba0/128ms64088 KiB
7Futási hiba0/227ms63996 KiB
8Futási hiba0/232ms63748 KiB
9Futási hiba0/232ms63508 KiB
10Futási hiba0/227ms63436 KiB
11Futási hiba0/132ms63284 KiB
12Futási hiba0/132ms63044 KiB
13Futási hiba0/132ms62808 KiB
14Futási hiba0/132ms62796 KiB
15Futási hiba0/332ms62556 KiB
16Futási hiba0/330ms62412 KiB
17Futási hiba0/330ms62400 KiB
18Futási hiba0/326ms62152 KiB
19Futási hiba0/330ms61936 KiB
20Futási hiba0/328ms61940 KiB
21Futási hiba0/330ms61940 KiB
22Futási hiba0/330ms61944 KiB
23Futási hiba0/327ms61948 KiB
24Futási hiba0/327ms61944 KiB
25Futási hiba0/226ms61876 KiB
26Futási hiba0/226ms61800 KiB