CF1324B Yet Another Palindrome Problem 题解

Original title link

CF 127 test points, praise

Briefly meaning of the questions:

A plurality of sets of data, asked whether there was a length of the array \ (\ geq 3 \) a palindromic sequence.

We need to find the essence.

Let us ask this title length, just let our judgment, this is why?

If the answer is YES, then there must be a length \ (3 \) palindromic sequence. Otherwise NO.

You think if the longest palindromic sequence of the original array is odd, then, as long as each simultaneously on both sides of the deletion of one, you can get a length \ (3 \) palindromic sequence.

If it is an even number, as long as each simultaneously on both sides of the deletion of one, then deleting an arbitrary two is in the middle, can be obtained length \ (3 \) palindromic sequence.

Whereas if not, then no.

for example:

10
1 2 3 4 2 1 4 2 4 3

You find this array longest palindromic sequence is:

3 4 2 2 4 3

Is even, then there must be a length where \ (3 \) is:

4 2 4

(Of course, more than one)

Therefore, the meaning of the questions read:

Determining whether there is an array of length \ (3 \) palindromic sequence.

? ? ? It is needless to judge it? ? ?

Length \ (3 \) , the intermediate-no limit, next to two numbers are equal.

That is, it is necessary to find the two numbers are equal, so that they are not adjacent (i.e., a number of intermediate let go further configured palindromic sequence) .

? ? ? It is needless to judge it? ? ?

Obviously, a Bo Haxi solve the problem. Just \ (a_i \ the n-Leq \) , this is not a godsend?

Time complexity: \ (O (T \ n-Times) \) .

Space complexity: \ (O (n-) \) .

Actual score: \ (100 pts \) .

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;

inline int read(){char ch=getchar();int f=1;while(ch<'0' || ch>'9') {if(ch=='-') f=-f; ch=getchar();}
	int x=0;while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*f;}

int a[5001],n;
int T;

int main(){
	T=read(); while(T--) {
		n=read(); bool f=0;
		memset(a,0,sizeof(a));
		for(int i=1,t;i<=n;i++) {
			t=read();
			if(a[t] && i-a[t]>1) f=1;
			if(!a[t]) a[t]=i;
		} if(f) puts("YES");
		else puts("NO");
	}
	return 0;
}

Guess you like

Origin www.cnblogs.com/bifanwen/p/12550080.html