当前位置:网站首页>B. Kalindrome Array
B. Kalindrome Array
2022-08-03 21:03:00 【秦小咩】
B. Kalindrome Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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
Copy
4 1 1 2 1 2 3 1 2 3 5 1 4 4 1 4
output
Copy
YES YES NO YES
Note
In the first test case, array [1][1] is already a palindrome, so it's a kalindrome as well.
In the second test case, we can choose x=2x=2, delete the second element, and obtain array [1][1], which is a palindrome.
In the third test case, it's impossible to obtain a palindrome.
In the fourth test case, you can choose x=4x=4 and delete the fifth element, obtaining [1,4,4,1][1,4,4,1]. You also can choose x=1x=1, delete the first and the fourth elements, and obtain [4,4,4][4,4,4].
双指针,左右相等继续推进,一旦不相等,就意味着必须删掉一个,而且答案就在这两者之间,所以枚举两者删除情况即可。题目忽悠人,说删几个x都行,其实必须都删才最好,因为如果两个x能左右对称那么删与不删一个样,一旦不对称还不如都删了。
#include <iostream>
# include<algorithm>
# include<cstring>
using namespace std;
typedef long long int ll;
int book[200000+10];
int b[200000+10];
int a[200000+10];
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
memset(book,0,sizeof(book));
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
if(n==1||n==2)
{
cout<<"YES"<<'\n';
continue;
}
int l=1,r=n;
int ans=0;
while(l<r)
{
if(a[l]==a[r])
{
l++;
r--;
}
else
{
int len=0;
for(int i=1;i<=n;i++)
{
if(a[i]!=a[l])
{
len++;
b[len]=a[i];
}
}
int flag=0;
for(int i=1;i<=len/2;i++)
{
if(b[i]!=b[len-i+1])
{
flag=1;
}
}
if(!flag)
{
ans=1;
break;
}
len=0;
for(int i=1;i<=n;i++)
{
if(a[i]!=a[r])
{
len++;
b[len]=a[i];
}
}
flag=0;
for(int i=1;i<=len/2;i++)
{
if(b[i]!=b[len-i+1])
{
flag=1;
}
}
if(!flag)
{
ans=1;
break;
}
break;
}
}
if(ans||l>=r)
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
return 0;
}
边栏推荐
- Orcad Capture Cadence 新建原理图多部分smybol和Homogeneous、Heterogeneous类型介绍教程
- 史兴国对谈于佳宁:从经济模式到落地应用,Web3的中国之路怎么走?
- Zero trust, which has been popular for more than ten years, why can't it be implemented?
- 详解虚拟机!京东大佬出品 HotSpot VM 源码剖析笔记(附完整源码)
- leetcode 136. Numbers that appear only once (XOR!!)
- abs()、fabs() 和 labs() 的区别
- ECCV 2022 | 清华&腾讯AI Lab提出REALY:重新思考3D人脸重建的评估方法
- LitJson报错记录
- 【使用 Pytorch 实现入门级的人工神经网络】
- 独立站卖家在哪些平台做社交媒体营销效果最好?
猜你喜欢
随机推荐
解决npm -v查看npm版本出现npm WARN config global `--global`, `--local` are deprecated. Use `--location报错
敏捷交付的工程效能治理
数据库定时备份winserver2012篇
双线性插值公式推导及Matlab实现
LitJson报错记录
【kali-漏洞扫描】(2.1)Nessus下载安装(上)
leetcode 326. Powers of 3
Cesium 修改鼠标样式
【kali-漏洞扫描】(2.1)Nessus解除IP限制、扫描快无结果、插件plugins被删除(中)
华为设备配置VRRP负载分担
剑指 Offer 16. 数值的整数次方
chart.js多条曲线图插件
【kali-漏洞利用】(3.2)Metasploit基础(上):基础知识
模板字符串
leetcode 268. 丢失的数字(异或!!)
Power button 206 - reverse list - the list
Leetcode 125. Verify palindrome string
CheckBox列表项选中动画js特效
服务器安装redis
Markdown语法