49012023-04-06 13:09:59SleepyOverlordKövetkező permutáció (50 pont)cpp17Időlimit túllépés 27/50337ms4404 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;

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

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

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

int main()
{
	cin >> n;
	a.RS(n + 1);
	tree.RS(4 * n + 1);
	biggestBefore.RS(n + 1);
	FOR(i, 1, n)
	{
		cin >> a[i];
		update(1, 1, n, i);
		biggestBefore[a[i]] = prefMax(1, 1, n, 1, 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(1, 1, n, 1, i - 1);
				update(1, 1, n, i);
				unused.erase(it);
				int firstBad = -1;
				FOR(j, i + 1, n)
				{
					if (good(*unused.BG))
					{
						a[j] = *unused.BG;
						biggestBefore[a[j]] = prefMax(1, 1, n, 1, j - 1);
						update(1, 1, n, j);
						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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base27/50
1Elfogadva0/03ms1812 KiB
2Időlimit túllépés0/0298ms2292 KiB
3Elfogadva1/13ms2544 KiB
4Elfogadva1/13ms2472 KiB
5Elfogadva1/13ms2656 KiB
6Elfogadva1/13ms2864 KiB
7Elfogadva2/22ms2912 KiB
8Elfogadva2/23ms2808 KiB
9Elfogadva2/23ms2936 KiB
10Elfogadva2/23ms3016 KiB
11Elfogadva1/13ms3152 KiB
12Elfogadva1/13ms3232 KiB
13Elfogadva1/13ms3232 KiB
14Elfogadva1/13ms3232 KiB
15Elfogadva3/34ms3236 KiB
16Elfogadva3/34ms3496 KiB
17Elfogadva3/3149ms3520 KiB
18Időlimit túllépés0/3270ms3532 KiB
19Időlimit túllépés0/3337ms3120 KiB
20Időlimit túllépés0/3272ms3296 KiB
21Időlimit túllépés0/3261ms4316 KiB
22Időlimit túllépés0/3272ms4404 KiB
23Időlimit túllépés0/3268ms3384 KiB
24Időlimit túllépés0/3264ms3452 KiB
25Időlimit túllépés0/2264ms3836 KiB
26Elfogadva2/28ms4264 KiB