QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#293876#6301. Minimum SuffixningagoTL 1ms5268kbC++144.3kb2023-12-29 21:35:532023-12-29 21:35:53

Judging History

你现在查看的是最新测评结果

  • [2023-12-29 21:35:53]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:5268kb
  • [2023-12-29 21:35:53]
  • 提交

answer

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <numeric>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <random> 

namespace uvu
{
//#define LOCAL ________________don_t_define_me_________________
#define ll long long
#define inf 0x3f3f3f3f
//#define int long long
//#define inf 0x3f3f3f3f3f3f3f3fll
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define gline debug("now is #%d\n", __LINE__)
#define pii std::pair <int, int>
#define mkp std::make_pair
#define fi first
#define se second
char _ST_;
const int BUFSIZE = 4000010;
char ibuf[BUFSIZE], *iS = ibuf, *iT = ibuf;
char obuf[BUFSIZE], *oS = obuf, *oT = obuf + BUFSIZE;
char getc()
{
#ifdef LOCAL
	return getchar();
#else
	if(iS == iT) iT = (iS = ibuf) + fread(ibuf, 1, BUFSIZE, stdin);
	return iS == iT ? EOF : *iS++;
#endif
}
void Flush()
{
	fwrite(obuf, 1, oS - obuf, stdout);
	oS = obuf;
}
struct Flusher{ ~Flusher() { Flush(); } }iamflusher;
void putc(char c)
{
#ifdef LOCAL
	putchar(c);
#else
	*oS++ = c;
	if(oS == oT) Flush();
#endif
}

template <typename T = int> T read()
{
	T x = 0, f = 1; char c = getc(); 
	for(; !isdigit(c); c = getc()) if(c == '-') f = -1;
	for(;  isdigit(c); c = getc()) x = (x << 3) + (x << 1) + (c ^ 48);
	return x * f;
}

template <typename T> void print(T x, char c)
{
static int sta[BUFSIZE], top;
	top = 0;
	if(x < 0) putc('-'), x = -x;
	if(!x) sta[++top] = 0;
	for(; x; x /= 10) sta[++top] = x % 10;
	for(; top; top--) putc(sta[top] ^ 48);
	if(c) putc(c);
}

int readstr(char *s, int base)
{
	int idx = base - 1; char c = getc();
	for(; !isdigit(c) && !isalpha(c) && c != '+' && c != '-'; c = getc());
	for(;  isdigit(c) ||  isalpha(c) || c == '+' || c == '-'; c = getc()) s[++idx] = c;
	return idx - base + 1;
}

void printstr(const char *s) { for(; *s; s++) putc(*s); }
void printf(const char *s) { printstr(s); }
template <typename T, typename ... Args> 
void printf(const char *s, T x, Args ... rest)
{
	for(; *s; s++)
	{
		if(*s != '%') { putc(*s); continue; }
		s++; if(*s == 'd') print(x, 0);
		else if(*s == 'c') putc(x);
		printf(s + 1, rest ...);
		return;
	}
}

template <typename T> void ckmax(T &x, T y) { x = x > y ? x : y; }
template <typename T> void ckmin(T &x, T y) { x = x < y ? x : y; }
template <typename T> T gcd(T x, T y) { for(; y; x %= y, x ^= y ^= x ^= y); return x; }
#define mod 998244353
//#define mod 1000000007
int sm(int x) { return x >= mod ? x - mod : x; } 
void plus_(int &x, int y) { x = sm(x + y); }
void mul_(int &x, int y) { x = 1ll * x * y % mod; }
int ksm(int a, int b) { int res = 1; for(; b; b >>= 1, mul_(a, a)) if(b & 1) mul_(res, a); return res; }

#define N 3000010
int n, m;
int p[N], last[N], s[N]; 
#define NO { printf("-1\n"); return; }
int pre[N]; bool val[N];
int lst(int x) { if(x > m) return 0; return last[x]; }

void solve()
{
//	memset(h, idx = -1, sizeof(h));
	m = 0;
	n = read(); for(int i = 1; i <= n; i++) p[i] = read();
	for(int r = n, l; r >= 1; r = l - 1)
	{
		val[l = p[r]] = 1; for(int i = l; i <= r; i++) if(p[i] < l) NO;
		for(int i = l + 1, len = 1; i <= r; i++)
		{
			pre[i] = i - len;
			if(p[i] == l) { val[i] = 1; len = i - l + 1; }
			else if(i - p[i] == i - len - p[i - len]) val[i] = 0;
			else NO;
		}
		bool big = 0;
		s[l] = lst(1);
		for(int i = l + 1, j; i <= r; i++)
		{
			s[i] = s[pre[i]] + val[i];
			if(s[i] > lst(i - l + 1)) big = 1;
//			printf("i = %d, big = %d, s[%d] = %d, lst = %d, val = %d\n", i, big, i, s[i], lst(i - l + 1), val[i]);
			if(!big && s[i] < lst(i - l + 1))
			{
				if(val[i]) s[i] = lst(i - l + 1);
				else { for(j = i; !val[j]; j--); s[i = j]++; }
			}
		}
//		printf("[%d %d] big = %d\n", l, r, big);
		if(!big && r - l + 1 < m)
		{
			int i, j;
			for(j = r; !val[j]; j--); s[i = j]++; big = 1;
			for(i++; i <= r; i++) s[i] = s[pre[i]] + val[i];
		}
		m = r - l + 1;
		for(int i = l; i <= r; i++) last[i - l + 1] = s[i];
	}
	for(int i = 1; i <= n; i++) print(s[i] + 1, ' '); putc('\n');
}

void init()
{
	
}

char _ED_;
void mian()
{
	debug("%.3f MB\n", abs(&_ST_ - &_ED_) / 1024.0 / 1024);
	init();
	for(int T = read(); T; solve(), T--);
}
#ifdef int
	#undef int
#endif
}

int main()
{
	uvu::mian();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5268kb

input:

6
3
1 1 1
3
1 1 2
3
1 1 3
3
1 2 1
3
1 2 2
3
1 2 3

output:

1 2 2 
-1
1 2 1 
1 1 2 
2 1 2 
1 1 1 

result:

ok 16 numbers

Test #2:

score: 0
Accepted
time: 1ms
memory: 5184kb

input:

2
2
1 1
2
1 2

output:

1 2 
1 1 

result:

ok 4 number(s): "1 2 1 1"

Test #3:

score: 0
Accepted
time: 1ms
memory: 5268kb

input:

24
4
1 1 1 1
4
1 1 1 2
4
1 1 1 3
4
1 1 1 4
4
1 1 2 1
4
1 1 2 2
4
1 1 2 3
4
1 1 2 4
4
1 1 3 1
4
1 1 3 2
4
1 1 3 3
4
1 1 3 4
4
1 2 1 1
4
1 2 1 2
4
1 2 1 3
4
1 2 1 4
4
1 2 2 1
4
1 2 2 2
4
1 2 2 3
4
1 2 2 4
4
1 2 3 1
4
1 2 3 2
4
1 2 3 3
4
1 2 3 4

output:

1 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1 1 3 1
6
1 1 1 1 3 2
6
1 1 1 1 3 3
6
1 1 1 1 3 4
6
1 1 1 1 3 5
6
1 1 1 1 3 6
6
1 1 1 1 4 1
6
1 1 1 1 4 2
6
1 1 1 1 4 3
6
...

output:

1 2 2 2 2 2 
-1
-1
-1
-1
1 2 2 2 2 1 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1 2 2 2 1 3 
-1
-1
-1
1 2 2 2 1 2 
1 2 2 2 1 1 
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-...

result:

ok 1165 numbers

Test #6:

score: -100
Time Limit Exceeded

input:

5040
7
1 1 1 1 1 1 1
7
1 1 1 1 1 1 2
7
1 1 1 1 1 1 3
7
1 1 1 1 1 1 4
7
1 1 1 1 1 1 5
7
1 1 1 1 1 1 6
7
1 1 1 1 1 1 7
7
1 1 1 1 1 2 1
7
1 1 1 1 1 2 2
7
1 1 1 1 1 2 3
7
1 1 1 1 1 2 4
7
1 1 1 1 1 2 5
7
1 1 1 1 1 2 6
7
1 1 1 1 1 2 7
7
1 1 1 1 1 3 1
7
1 1 1 1 1 3 2
7
1 1 1 1 1 3 3
7
1 1 1 1 1 3 4
7
1 1 1...

output:


result: