An array [b1,b2,…,bm][b1,b2,…,bm] is a palindrome, if bi=bm+1−ibi=bm+1−i for each ii from 11 to mm. Empty array is also a palindrome.
An array is called kalindrome, if the following condition holds:
-
It's possible to select some integer xx and delete some of the elements of the array equal to xx, so that the remaining array (after gluing together the remaining parts) is a palindrome.
Note that you don't have to delete all elements equal to xx, and you don't have to delete at least one element equal to xx.
For example :
- [1,2,1][1,2,1] is kalindrome because you can simply not delete a single element.
- [3,1,2,3,1][3,1,2,3,1] is kalindrome because you can choose x=3x=3 and delete both elements equal to 33, obtaining array [1,2,1][1,2,1], which is a palindrome.
- [1,2,3][1,2,3] is not kalindrome.
You are given an array [a1,a2,…,an][a1,a2,…,an]. Determine if aa is kalindrome or not.
Input
The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of the array.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n) — elements of the array.
It's guaranteed that the sum of nn over all test cases won't exceed 2⋅1052⋅105.
Output
For each test case, print YES if aa is kalindrome and NO otherwise. You can print each letter in any case.
Example
input
4 1 1 2 1 2 3 1 2 3 5 1 4 4 1 4
output
YES YES NO YES
题意:如果一串数字本身满足回文或者删除某一个数字之后满足回文即为符合要求的数组
思路:应用双指针算法,同时从头和尾扫描,直到两个数不同,那么此两个数即为删除的考虑对象,进行枚举即可
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
const int N = 2 * 1e5 + 10;
int n, m, t, a[N];
bool check(vector<int> &v)
{
for(int i = 0; i < v.size(); i ++)
if(v[i] != v[v.size() - 1 - i]) return false;
return true;
}//判断是否合格
int main()
{
cin >> t;
while(t --)
{
cin >> n;
for(int i = 1;i <= n; i++) cin >> a[i];
int l = 1, r = n;
while(l < r && a[l] == a[r])l ++,r --;
int num1 = a[l], num2 = a[r];
vector<int> v1,v2;
for(int i = 1; i <= n; i ++)
if(a[i] != num1) v1.push_back(a[i]);
//将删除对应数字的数组放到新的数组中
for(int i = 1; i <= n; i ++)
if(a[i] != num2) v2.push_back(a[i]);
if(check(v1) || check(v2)) printf("Yes\n");
else printf("No\n");
}
return 0;
}
(萌新上路,有更好的方法或者写的有问题可以多多交流)