QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#640869#5190. 小 E 爱消除DengDuckAC ✓293ms12984kbC++142.1kb2024-10-14 16:36:242024-10-14 16:36:24

Judging History

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

  • [2024-10-14 16:36:24]
  • 评测
  • 测评结果:AC
  • 用时:293ms
  • 内存:12984kb
  • [2024-10-14 16:36:24]
  • 提交

answer

#include<bits/stdc++.h>
#define pII pair<int,int>
#define Fi first
#define Se second
using namespace std;
const int N=55;
const int Inf=1e8;
int n,A[N],H[N],S[N],F[N][N][N][N];
pII G[N][N];
inline pII operator+(pII A,pII B){return {A.Fi+B.Fi,A.Se+B.Se};}
int Dfs(int L1,int R1,int L2,int R2)
{
	if(L1>R1&&L2>R2)return 0;
	if((L1+L2+R1+R2)&1)return Inf;
	if(S[R1]^S[L1-1]^S[R2]^S[L2-1])return Inf;
	if(L1>R1)L1=1,R1=0;
	if(L2>R2)L2=1,R2=0;
	int &Ans=F[L1][R1][L2][R2];
	if(Ans)return Ans;
	Ans=Inf;
	for(int i=L1;i<=R1;i++)
	{
		if(i!=L1&&A[i]==A[L1])
		{
			for(int j=L2-1;j<=R2;j++)
			{
				Ans=min(Ans,max({Dfs(L1+1,i-1,j+1,R2)+1,2,Dfs(i+1,R1,L2,j)}));
			}
		}
		if(L2<=R2&&A[i]==A[R2])
		{
			for(int j=L2-1;j<R2;j++)
			{
				Ans=min(Ans,max({Dfs(L1,i-1,j+1,R2-1)+1,2,Dfs(i+1,R1,L2,j)}));
			}
		}
	}
	for(int i=L2;i<=R2;i++)
	{
		if(i!=R2&&A[i]==A[R2])
		{
			for(int j=L1;j<=R1+1;j++)
			{
				Ans=min(Ans,max({Dfs(L1,j-1,i+1,R2-1)+1,2,Dfs(j,R1,L2,i-1)}));
			}
		}
		if(L1<=R1&&A[i]==A[L1])
		{
			for(int j=L1+1;j<=R1+1;j++)
			{
				Ans=min(Ans,max({Dfs(L1+1,j-1,i+1,R2)+1,2,Dfs(j,R1,L2,i-1)}));
			}
		}
	}
	return Ans;
}
#define Mk make_pair
pII Dfs(int L,int R)
{
	if(L>R)return Mk(0,0);
	pII &Ans=G[L][R];
	if(Ans.Fi||Ans.Se)return Ans;
	Ans=Mk(Inf,Inf);
	Ans=min({Ans,Dfs(L,R-1)+Mk(1,1),Dfs(L+1,R)+Mk(1,1)});
	for(int i=L;i<=R;i++)
	{
		if(i!=L&&A[i]==A[L])
		{
			for(int j=L+1;j<=R;j++)
			{
				int K=Dfs(L+1,min(i,j)-1,max(i,j)+1,R);
				if(K==Inf)continue;
				pII T;
				if(i>=j)T=Dfs(j,i-1);
				else T=Dfs(i+1,j);
				Ans=min(Ans,Mk(T.Fi,max({T.Se,2,K+1})));
			}
		}
		if(i!=R&&A[i]==A[R])
		{
			for(int j=L;j<=R-1;j++)
			{
				int K=Dfs(L,min(i,j)-1,max(i,j)+1,R-1);
				if(K==Inf)continue;
				pII T;
				if(i>=j)T=Dfs(j,i-1);
				else T=Dfs(i+1,j);
				Ans=min(Ans,Mk(T.Fi,max({T.Se,2,K+1})));
			}	
		}
	}
	return Ans;
}
mt19937 Rnd(time(0));
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&A[i]);
		H[i]=Rnd();
	}
	for(int i=1;i<=n;i++)S[i]=S[i-1]^H[A[i]];
	pII Ans=Dfs(1,n);
	printf("%d %d",Ans.Fi,Ans.Se);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 83ms
memory: 12456kb

input:

50
14 31 14 31 14 31 14 14 14 31 14 14 14 31 31 31 14 31 31 31 31 14 31 14 31 14 31 14 31 31 14 31 14 14 31 31 14 31 31 14 14 31 31 14 31 31 14 14 31 14

output:

0 3

result:

ok single line: '0 3'

Test #2:

score: 0
Accepted
time: 31ms
memory: 11696kb

input:

50
1 18 18 18 50 1 50 1 18 18 50 18 18 18 50 1 50 1 18 50 18 50 50 50 18 18 18 50 50 1 18 18 18 18 50 1 50 1 50 50 18 1 50 50 50 1 1 18 18 50

output:

2 3

result:

ok single line: '2 3'

Test #3:

score: 0
Accepted
time: 5ms
memory: 7648kb

input:

50
6 5 13 5 36 13 29 29 29 5 36 5 6 13 5 5 6 5 29 29 5 5 29 5 13 6 13 13 5 13 5 13 29 39 39 39 6 6 6 39 5 39 13 13 29 13 5 6 6 39

output:

2 8

result:

ok single line: '2 8'

Test #4:

score: 0
Accepted
time: 0ms
memory: 4688kb

input:

50
50 34 43 39 50 21 50 27 34 7 50 2 7 27 2 43 21 43 7 7 39 34 7 32 50 34 50 34 32 34 32 34 21 27 7 2 50 34 27 39 39 7 21 21 7 27 39 50 34 7

output:

10 11

result:

ok single line: '10 11'

Test #5:

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

input:

50
10 49 41 28 26 11 5 24 29 18 39 38 13 16 26 37 40 25 50 44 26 13 49 24 2 6 4 3 38 4 15 37 41 37 34 25 20 24 15 38 26 15 26 10 6 7 47 13 31 6

output:

38 38

result:

ok single line: '38 38'

Test #6:

score: 0
Accepted
time: 0ms
memory: 3864kb

input:

1
1

output:

1 1

result:

ok single line: '1 1'

Test #7:

score: 0
Accepted
time: 293ms
memory: 12984kb

input:

50
22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22

output:

0 2

result:

ok single line: '0 2'

Test #8:

score: 0
Accepted
time: 3ms
memory: 5920kb

input:

50
19 40 9 10 19 40 12 9 19 19 7 9 9 7 12 19 19 7 19 40 46 12 10 46 12 7 19 7 19 12 7 10 7 46 12 7 9 9 10 12 7 7 10 46 19 12 7 9 10 40

output:

6 9

result:

ok single line: '6 9'

Test #9:

score: 0
Accepted
time: 0ms
memory: 6740kb

input:

50
26 9 1 1 1 29 1 9 26 20 20 21 1 26 21 21 1 1 29 26 29 21 1 1 21 1 26 21 21 1 26 20 9 1 29 29 29 21 29 20 29 21 20 29 1 1 9 9 29 29

output:

4 5

result:

ok single line: '4 5'

Test #10:

score: 0
Accepted
time: 2ms
memory: 5132kb

input:

50
12 45 45 50 24 45 45 5 50 50 24 24 45 31 5 9 24 24 31 24 24 12 45 5 12 12 32 45 9 45 32 31 5 50 12 32 24 50 5 9 32 50 9 31 9 9 32 31 50 45

output:

14 14

result:

ok single line: '14 14'

Test #11:

score: 0
Accepted
time: 86ms
memory: 12520kb

input:

50
42 32 42 42 42 42 42 32 42 32 42 42 42 42 42 42 32 32 32 32 42 42 32 32 42 32 42 32 42 42 32 32 42 32 32 32 42 32 32 32 32 42 42 32 32 42 32 42 32 42

output:

0 3

result:

ok single line: '0 3'

Test #12:

score: 0
Accepted
time: 2ms
memory: 5360kb

input:

50
47 47 12 38 43 9 12 49 47 47 38 43 47 43 37 38 9 12 38 38 47 37 9 9 37 38 38 12 47 12 12 24 49 43 43 43 49 49 47 43 38 24 47 47 43 9 12 37 9 12

output:

0 7

result:

ok single line: '0 7'

Test #13:

score: 0
Accepted
time: 4ms
memory: 7508kb

input:

50
2 40 16 35 7 38 40 40 40 7 16 7 40 2 38 16 16 38 2 38 40 2 40 2 40 7 2 2 2 40 2 38 40 38 16 35 2 2 38 40 7 7 7 2 2 16 40 38 2 7

output:

0 6

result:

ok single line: '0 6'

Test #14:

score: 0
Accepted
time: 3ms
memory: 5608kb

input:

50
37 37 36 15 13 13 36 36 13 13 35 36 36 3 13 29 37 36 36 21 35 13 35 35 36 21 15 15 36 37 37 21 37 21 21 29 3 3 35 36 29 29 3 36 36 3 3 35 15 21

output:

0 5

result:

ok single line: '0 5'

Test #15:

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

input:

50
37 9 8 16 16 9 9 30 36 6 37 16 23 30 8 50 1 9 16 1 23 23 1 1 22 38 38 16 1 37 8 16 1 9 9 8 8 1 22 30 22 23 37 6 36 30 8 50 1 22

output:

0 9

result:

ok single line: '0 9'

Test #16:

score: 0
Accepted
time: 68ms
memory: 12260kb

input:

50
5 5 5 5 5 5 5 5 5 5 5 5 3 3 4 4 5 4 4 5 4 4 5 4 4 5 4 4 5 4 4 5 4 4 5 4 4 5 4 4 5 4 4 5 4 4 5 4 4 5

output:

0 2

result:

ok single line: '0 2'

Test #17:

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

input:

50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 47 24 1 49 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

output:

6 25

result:

ok single line: '6 25'

Test #18:

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

input:

50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 47 24 1 2 47 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

output:

0 25

result:

ok single line: '0 25'

Test #19:

score: 0
Accepted
time: 0ms
memory: 6140kb

input:

50
1 2 3 4 5 6 2 3 4 5 6 7 3 4 5 6 7 8 4 5 6 7 8 9 5 1 2 3 4 5 6 2 3 4 5 6 7 3 4 5 6 7 8 4 5 6 7 8 9 5

output:

0 14

result:

ok single line: '0 14'

Test #20:

score: 0
Accepted
time: 0ms
memory: 7992kb

input:

50
1 3 5 7 9 11 3 7 3 9 11 1 5 1 1 9 7 7 3 5 3 7 11 9 5 7 1 3 11 5 9 3 5 11 9 1 11 5 1 7 7 3 9 11 9 3 5 11 9 7

output:

4 8

result:

ok single line: '4 8'