49042023-04-06 15:52:56SleepyOverlordKövetkező permutáció (50 pont)cpp17Időlimit túllépés 48/50261ms5480 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, 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()
{
	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)
				{
					auto it2 = unused.BG;
					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
base48/50
1Elfogadva0/03ms1812 KiB
2Elfogadva0/08ms2680 KiB
3Elfogadva1/13ms2364 KiB
4Elfogadva1/13ms2488 KiB
5Elfogadva1/12ms2688 KiB
6Elfogadva1/13ms2892 KiB
7Elfogadva2/23ms3144 KiB
8Elfogadva2/23ms3360 KiB
9Elfogadva2/23ms3496 KiB
10Elfogadva2/23ms3740 KiB
11Elfogadva1/13ms3820 KiB
12Elfogadva1/13ms3924 KiB
13Elfogadva1/13ms4172 KiB
14Elfogadva1/13ms4288 KiB
15Elfogadva3/33ms4320 KiB
16Elfogadva3/33ms4404 KiB
17Elfogadva3/33ms4456 KiB
18Elfogadva3/34ms4500 KiB
19Elfogadva3/313ms5084 KiB
20Elfogadva3/38ms5096 KiB
21Elfogadva3/39ms4992 KiB
22Elfogadva3/312ms5268 KiB
23Elfogadva3/310ms5468 KiB
24Elfogadva3/310ms5440 KiB
25Időlimit túllépés0/2261ms4764 KiB
26Elfogadva2/29ms5480 KiB