• B. Kalindrome Array


    Problem - B - Codeforces

    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能左右对称那么删与不删一个样,一旦不对称还不如都删了。

    1. #include
    2. # include
    3. # include
    4. using namespace std;
    5. typedef long long int ll;
    6. int book[200000+10];
    7. int b[200000+10];
    8. int a[200000+10];
    9. int main()
    10. {
    11. int t;
    12. cin>>t;
    13. while(t--)
    14. {
    15. int n;
    16. cin>>n;
    17. memset(book,0,sizeof(book));
    18. for(int i=1;i<=n;i++)
    19. {
    20. cin>>a[i];
    21. }
    22. if(n==1||n==2)
    23. {
    24. cout<<"YES"<<'\n';
    25. continue;
    26. }
    27. int l=1,r=n;
    28. int ans=0;
    29. while(l
    30. {
    31. if(a[l]==a[r])
    32. {
    33. l++;
    34. r--;
    35. }
    36. else
    37. {
    38. int len=0;
    39. for(int i=1;i<=n;i++)
    40. {
    41. if(a[i]!=a[l])
    42. {
    43. len++;
    44. b[len]=a[i];
    45. }
    46. }
    47. int flag=0;
    48. for(int i=1;i<=len/2;i++)
    49. {
    50. if(b[i]!=b[len-i+1])
    51. {
    52. flag=1;
    53. }
    54. }
    55. if(!flag)
    56. {
    57. ans=1;
    58. break;
    59. }
    60. len=0;
    61. for(int i=1;i<=n;i++)
    62. {
    63. if(a[i]!=a[r])
    64. {
    65. len++;
    66. b[len]=a[i];
    67. }
    68. }
    69. flag=0;
    70. for(int i=1;i<=len/2;i++)
    71. {
    72. if(b[i]!=b[len-i+1])
    73. {
    74. flag=1;
    75. }
    76. }
    77. if(!flag)
    78. {
    79. ans=1;
    80. break;
    81. }
    82. break;
    83. }
    84. }
    85. if(ans||l>=r)
    86. {
    87. cout<<"YES"<
    88. }
    89. else
    90. {
    91. cout<<"NO"<
    92. }
    93. }
    94. return 0;
    95. }

  • 相关阅读:
    差值结构的顺序偏好
    学习笔记-Nginx
    字节跳动软件测试岗,前两面过了,第三面HR天坑,结局透心凉...
    YOLOv6: A Single-Stage Object Detection Framework for IndustrialApplications
    SQL语句查询关键字
    django项目实战基于Python实现的飞机票销售系统
    蒙特卡洛模拟法计算电动汽车充电负荷(Matlab代码实现)
    pyqt 信号槽函数传递失败
    先获取打印的模板,用户选择过的模板通过接口要回显出来,预览只显示key的值,value是前端写死
    【日积月累】Java开发习惯养成
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126128819