Codeforces Round #793 (Div. 2)

本文解析了Codeforces Round #793 (Div.2)中的ABC三题,包括计算回文字符串移除字符后仍为回文的计数,寻找X排序的数组最大X值,以及求解数组重新排列后的最长上升与下降子序列美丽值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


Codeforces Round #793 (Div. 2)

前言

Codeforces Round #793 (Div. 2)的ABC题


二、正文

A. Palindromic Indices

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a palindromic string s of length n.

You have to count the number of indices i (1≤i≤n) such that the string after removing si from s still remains a palindrome.

For example, consider s = “aba”

If we remove s1 from s, the string becomes “ba” which is not a palindrome.
If we remove s2 from s, the string becomes “aa” which is a palindrome.
If we remove s3 from s, the string becomes “ab” which is not a palindrome.
A palindrome is a string that reads the same backward as forward. For example, “abba”, “a”, “fef” are palindromes whereas “codeforces”, “acd”, “xy” are not.

Input
The input consists of multiple test cases. The first line of the input contains a single integer t (1≤t≤103) — the number of test cases. Description of the test cases follows.

The first line of each testcase contains a single integer n (2≤n≤105) — the length of string s.

The second line of each test case contains a string s consisting of lowercase English letters. It is guaranteed that s is a palindrome.

It is guaranteed that sum of n over all test cases does not exceed 2⋅105.

Output
For each test case, output a single integer — the number of indices i (1≤i≤n) such that the string after removing si from s still remains a palindrome.

Example
inputCopy
3
3
aba
8
acaaaaca
2
dd
outputCopy
1
4
2
Note
The first test case is described in the statement.

In the second test case, the indices i that result in palindrome after removing si are 3,4,5,6. Hence the answer is 4.

In the third test case, removal of any of the indices results in “d” which is a palindrome. Hence the answer is 2.
题意
给你一个长度n的回文字符串,可以每次移除一个字符,但移除的字符必须是同一种字符,并且移除后字符串还是回文字符串。
问可以移除字符个数?
题解
移除只能一处中间的,如aba只能移除b,这样移除后还是回文。所以n/2找的回文的中心轴,找左边或右边相同字符的一个数量。
如果n是偶数,数量2输出,n是奇数数量2+1(中间的加上)

#include<iostream>
#include<string>
using namespace std;
int main()
{
 
    int t;
    cin>>t;
    while(t--)
    {
 
 
        string a;
 
        int len,s=0,l;
        cin>>len;
        cin>>a;
 
        l=(len-1)/2;
        char f=a[l];
        while(l>=0)
        {
            if(f==a[l])
            {
 
                s++;
            }
            else {
                break;
            }
            l--;
 
        }
        if(len%2==0)
        {
 
        s=s*2;
 
        }else s=(s-1)*2+1;
        if(len==1)
            cout<<1<<endl;
        else
        cout<<s<<endl;
 
    }
}

B. AND Sorting

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a permutation p of integers from 0 to n−1 (each of them occurs exactly once). Initially, the permutation is not sorted (that is, pi>pi+1 for at least one 1≤i≤n−1).

The permutation is called X-sortable for some non-negative integer X if it is possible to sort the permutation by performing the operation below some finite number of times:

Choose two indices i and j (1≤i<j≤n) such that pi&pj=X.
Swap pi and pj.
Here & denotes the bitwise AND operation.

Find the maximum value of X such that p is X-sortable. It can be shown that there always exists some value of X such that p is X-sortable.

Input
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤104) — the number of test cases. Description of test cases follows.

The first line of each test case contains a single integer n (2≤n≤2⋅105) — the length of the permutation.

The second line of each test case contains n integers p1,p2,…,pn (0≤pi≤n−1, all pi are distinct) — the elements of p. It is guaranteed that p is not sorted.

It is guaranteed that the sum of n over all cases does not exceed 2⋅105.

Output
For each test case output a single integer — the maximum value of X such that p is X-sortable.

Example
inputCopy
4
4
0 1 3 2
2
1 0
7
0 1 2 3 5 6 4
5
0 3 2 1 4
outputCopy
2
0
4
1
Note
In the first test case, the only X for which the permutation is X-sortable are X=0 and X=2, maximum of which is 2.

Sorting using X=0:

Swap p1 and p4, p=[2,1,3,0].
Swap p3 and p4, p=[2,1,0,3].
Swap p1 and p3, p=[0,1,2,3].
Sorting using X=2:

Swap p3 and p4, p=[0,1,2,3].
In the second test case, we must swap p1 and p2 which is possible only with X=0.

#include<iostream>
#include<string>
using namespace std;
int main()
{
 
    int t;
    cin>>t;
    while(t--)
    {
 
 
int  n,a[200005],s;
cin>>n;
for(int i=0;i<n;++i)
    cin>>a[i];
s=(1<<30)-1;
for(int i=0;i<n;++i)
{
 
    if(i!=a[i])
        s=s&a[i];
}
cout<<s<<endl;
    }
}

题意
给一个乱序的数组,可以进行如下操作:
选择a[i],a[j],a[i]&a[j]=X,然后进行交换
是数组有序问X最大值?
题解
首先题意说可以任选a[i],a[j],只要a[i]&a[j]=X,就可以交换,交换一定次数是数组有序。
对于交换,如果随意交换的话X是不相等的,如3,1,2, 1&2!=2&3.所以要选取中间值交换如
0,1,2,4,3
4和0交换:4,1,2,0,3
3和0交换:4,1,2,3,0
4和0交换:0,1,2,3,4
中间值位置不变,但成功交换了位置,由于1&0=0,所以4&0=0,3&0=0,X值相同,这种操作是可以的。
但是问题求的是最大值,0明显是最小的。通过&性质可知:对所有的错位的,如6,4,5
6二进制:110
5二进制:101
4二进制:100
想要X最大,6&X=5&X=4&X,不用把所有位变成0,只需每位上不同的变成零,相同变成1,如最左全是1,保留1,后两位有0有1,变成零即X=100
这样X最大。对于所有错位的数,相应位二进制全是1的位1,有0有1的为0,很明应是&操作。对所有错位数进行&

C. LIS or Reverse LIS?

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array a of n positive integers.

Let LIS(a) denote the length of longest strictly increasing subsequence of a. For example,

LIS([2,1–,1,3–]) = 2.
LIS([3–,5–,10–––,20–––]) = 4.
LIS([3,1–,2–,4–]) = 3.
We define array a′ as the array obtained after reversing the array a i.e. a′=[an,an−1,…,a1].

The beauty of array a is defined as min(LIS(a),LIS(a′)).

Your task is to determine the maximum possible beauty of the array a if you can rearrange the array a arbitrarily.

Input
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤104) — the number of test cases. Description of the test cases follows.

The first line of each test case contains a single integer n (1≤n≤2⋅105) — the length of array a.

The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109) — the elements of the array a.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output
For each test case, output a single integer — the maximum possible beauty of a after rearranging its elements arbitrarily.

Example
inputCopy
3
3
6 6 6
6
2 5 4 5 2 4
4
1 3 2 2
outputCopy
1
3
2
Note
In the first test case, a = [6,6,6] and a′ = [6,6,6]. LIS(a)=LIS(a′) = 1. Hence the beauty is min(1,1)=1.

In the second test case, a can be rearranged to [2,5,4,5,4,2]. Then a′ = [2,4,5,4,5,2]. LIS(a)=LIS(a′)=3. Hence the beauty is 3 and it can be shown that this is the maximum possible beauty.

In the third test case, a can be rearranged to [1,2,3,2]. Then a′ = [2,3,2,1]. LIS(a)=3, LIS(a′)=2. Hence the beauty is min(3,2)=2 and it can be shown that 2 is the maximum possible beauty.

#include<iostream>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
map<int,int>map1;
int main()
{
 
    int t;
    cin>>t;
    while(t--)
    {
        map1.clear();
int n,s=0;
cin>>n;
for(int i=0;i<n;++i)
{
    int l;
    cin>>l;
    map1[l]++;
    if(map1[l]<=2)
        s++;
}
 
 
cout<<(s-1)/2+1<<endl;
    }
}

题意
给一个数组,可以对数组任意排序,要求:最长上升子序列和最长下降子序列长度最小值的最大值。
题解
将数组变成一个山形(先升后降)可求最大值,上升长度下降长度取最小,所以让两边尽可能相等,如果数组种有两个数一样,可以分别放两边。如果大于2,2 2既不是上升也不下降,所以舍弃。将数组元素相同个数大于2的舍弃,剩余数组元素/2(奇数+1)就是结果

总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值