CF1610B. Kalindrome Array

CF1610B. Kalindrome Array

Lionlnuc
Lionlnuc Lv4

题目链接: Problem - 1610B - Codeforces
题意:
判断一个数组是否是kalindrome(卡林德数组)。
如果删去 k 个数组中等于 x 的元素能使其变为回文数组则为卡林德数组。k 可以等于 0。
思路:
如果一个数组刚开始就是回文数组,那么直接输出 YES。
如果不是的话就开始从左右两边向里推进找到对称位置不相同的元素。可以发现,若存在一种不完全的删数方法,那么所有的数全都删掉也一定是可以的,这样就可以简化判断了。
而且只会删两端第一次不匹配的两种数(否则一定不合法)。
代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
const int N=2e5+5;
int a[N];
int b[N];
int c[N];
bool is_hw(int a[],int cnt)
{
for(int i=1;i<=cnt/2;i++)
{
if(a[i]!=a[cnt-i+1])return false;
}
return true;
}
void solve()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
if(is_hw(a,n)){
cout<<"YES"<<endl;
return;
}
int x,y;
for(int i=1;i<=n;i++)
{
if(a[i]!=a[n-i+1])
{
x=a[i];
y=a[n-i+1];
break;
}
}
int cnt1=0,cnt2=0;
//下面构造一下删去两种元素后的数组
for(int i=1;i<=n;i++)
{
if(a[i]!=x)b[++cnt1]=a[i];
if(a[i]!=y)c[++cnt2]=a[i];
}
if(is_hw(b,cnt1)||is_hw(c,cnt2))cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
signed main(){
IOS
int t;
cin>>t;
//int t=1;
while(t--)
{
solve();
}
}

认为这种简单又粗糙的方式很顺手☺️☺️

  • 标题: CF1610B. Kalindrome Array
  • 作者: Lionlnuc
  • 创建于 : 2026-02-01 00:00:00
  • 更新于 : 2026-02-01 00:00:00
  • 链接: https://lionlnuc.github.io/2026/02/01/CF1610B. Kalindrome Array/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
目录
CF1610B. Kalindrome Array