https://codeforces.com/problemset/problem/1610/B
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
using ll = long long;
using pii = pair<int, int>;
const double PI = acos(-1);
const int N = 2e5+10;
const int mod = 1e9 + 7;
int a[N];
void solve() {
int n;cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
int l=1,r=n;
int x=0,y=0;
bool flag=0;
while(l<r){
if(a[l]==a[r]){
l++;
r--;
}
else{
x=a[l];
y=a[r];
flag=1;
break;
}
}
if(!flag){
cout<<"YES"<<endl;
return;
}
l=1,r=n;
bool flag1=0,flag2=0;
while(l<r){
if(a[l]==a[r]){
l++;
r--;
}
else{
if(a[l]==x) l++;
else if(a[r]==x) r--;
else {
flag1=1;
break;
}
}
}
l=1,r=n;
while(l<r){
if(a[l]==a[r]){
l++;
r--;
}
else{
if(a[l]==y) l++;
else if(a[r]==y) r--;
else {
flag2=1;
break;
}
}
}
if(flag1==1&&flag2==1){
cout<<"NO"<<endl;
}
else cout<<"YES"<<endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int T = 1;
cin>>T;
while (T--) {
solve();
}
return 0;
}
标签:const,int,long,while,solve,数组,using,回文,指针
From: https://www.cnblogs.com/laileou/p/18675867