Codeforces Round #750 (Div. 2) CD

C

Description

Grandma Capa has decided to knit a scarf and asked Grandpa Sher to make a pattern for it, a pattern is a string consisting of lowercase English letters. Grandpa Sher wrote a string s s s of length n n n.

Grandma Capa wants to knit a beautiful scarf, and in her opinion, a beautiful scarf can only be knit from a string that is a palindrome. She wants to change the pattern written by Grandpa Sher, but to avoid offending him, she will choose one lowercase English letter and erase some (at her choice, possibly none or all) occurrences of that letter in string s s s.

She also wants to minimize the number of erased symbols from the pattern. Please help her and find the minimum number of symbols she can erase to make string s s s a palindrome, or tell her that it’s impossible. Notice that she can only erase symbols equal to the one letter she chose.

A string is a palindrome if it is the same from the left to the right and from the right to the left. For example, the strings ‘``kek', 'abacaba', 'r' and 'papicipap' are palindromes, while the strings 'abb' and 'iq`’ are not.

Input

The first line contains a single integer t ( 1 ≤ t ≤ 100 ) t (1≤t≤100) t(1t100) — the number of test cases. The next 2 ⋅ t 2⋅t 2t lines contain the description of test cases. The description of each test case consists of two lines.

The first line of each test case contains a single integer n ( 1 ≤ n ≤ 1 0 5 ) n (1≤n≤10^5) n(1n105) — the length of the string.

The second line of each test case contains the string s s s consisting of n n n lowercase English letters.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2⋅10^5 2105.

Output

For each test case print the minimum number of erased symbols required to make the string a palindrome, if it is possible, and −1, if it is impossible.

Solution

1.可以直接模拟只删除某个字符的情况,复杂度为 O ( 26 n ) O(26n) O(26n)

2.具体实现细节可以使用双指针,当两个指针的值相同时可以各向中间靠一步;否则当有一方为指定字符时可以删去并更新指针;如果都不是则无法实现(返回一个很大的值)。

Code

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define MOD 1000000007
#define intmax 2147483647
#define memmax 0x7fffffff

inline ll read()
{
	ll x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

ll t;
ll n;
string s;

ll work(char ch)
{
    ll num = 0;

    int left = 0, right = s.length()-1;
    while(left < right)
    {
        if(s[left] == s[right])
            left++, right--;
        else
        {
            if(s[left] == ch)
                left++, num++;
            else if(s[right] == ch)
                right--, num++;
            else// 都不是且不相同
                return 1ll*MOD;
        }
    }

    return num;
}

void solve()
{
    cin >> n;
    cin >> s;

    ll ans = MOD;
    for(char ch='a'; ch<='z'; ch++)
        ans = min(ans, work(ch));

    if(ans == MOD)
        cout << -1 << endl;
    else
        cout << ans << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> t;
    while(t--)
        solve();
}

D

Description

Vupsen and Pupsen were gifted an integer array. Since Vupsen doesn’t like the number 0 0 0, he threw away all numbers equal to 0 0 0 from the array. As a result, he got an array a a a of length n n n.

Pupsen, on the contrary, likes the number 0 0 0 and he got upset when he saw the array without zeroes. To cheer Pupsen up, Vupsen decided to come up with another array b b b of length n n n such that ∑ i = 1 n a i ⋅ b i = 0 ∑_{i=1}{n}a_i⋅b_i=0 i=1naibi=0. Since Vupsen doesn’t like number 0 0 0, the array b b b must not contain numbers equal to 0 0 0. Also, the numbers in that array must not be huge, so the sum of their absolute values cannot exceed 1 0 9 10^9 109. Please help Vupsen to find any such array b b b!

Input

The first line contains a single integer t ( 1 ≤ t ≤ 100 ) t (1≤t≤100) t(1t100) — the number of test cases. The next 2 ⋅ t 2⋅t 2t lines contain the description of test cases. The description of each test case consists of two lines.

The first line of each test case contains a single integer n ( 2 ≤ n ≤ 1 0 5 ) n (2≤n≤10^5) n(2n105) — the length of the array.

The second line contains n n n integers a 1 , a 2 , … , a n ( − 1 0 4 ≤ a i ≤ 1 0 4 , a i ≠ 0 ) a_1,a_2,…,a_n (−10^4≤a_i≤10^4, a_i≠0) a1,a2,,an(104ai104,ai=0) — the elements of the array a a a.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2⋅10^5 2105.

Output

For each test case print n n n integers b 1 , b 2 , … , b n b_1,b_2,…,b_n b1,b2,,bn — elements of the array b ( ∣ b 1 ∣ + ∣ b 2 ∣ + … + ∣ b n ∣ ≤ 1 0 9 , b i ≠ 0 , ∑ i = 1 n a i ⋅ b i = 0 ) b (|b_1|+|b_2|+…+|b_n|≤10^9, b_i≠0, ∑_{i=1}^{n}a_i⋅b_i=0) b(b1+b2++bn109,bi=0,i=1naibi=0).

It can be shown that the answer always exists.

Solution

1.可以贪心地考虑,将数组分为小区间,保证构造的小区间积和为0

2.讨论n的奇偶性

n大于1,因此1不做考虑

n为偶数

将数组分为两两一组,记为ab ,此时可以对应构造 -b , a.这样就可以保证a*(-b)+b*a = 0

n为奇数

取前三个或者后三个数,剩下为偶数个数按照偶数做法即可。

下面讨论这三个数积和为0的做法:

令这三个数为 a, b, c

  1. 先找出两个和不为0的数,显然这样的情况存在。

证明:

假设不存在,则a+b=0,a+c=0,推出b=c=-a,则b+c == 0当且仅当 b==0, c==0,与题意不符,证毕。

  1. 运用偶数做法思想

假设a+b != 0,对于a, b, c ,可以构造 -c, -c, a+b,使得 a*(-c)+b*(-c)+c*(a+b) == 0

条件限定

对于偶数做法,由于 |a|+|b| == |-b|+|a|,a和b数组的绝对值和相同,不超范围;

对于奇数,只多一个c,这里没有严格证明,但应该没有更优解了,出题人不会在这里卡。

Code

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define MOD 1000000007
#define intmax 2147483647
#define memmax 0x7fffffff

inline ll read()
{
	ll x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

ll t;
ll n;
ll arr[100005];
vector<ll>v;


void solve()
{
    v.clear();

    n = read();
    for(int i=1; i<=n; i++)
        arr[i] = read();

    if(n & 1)
    {
        // deal with the element 
        // except the last three elements
        for(int i=1; i<=n-4; i+=2)
        {
            v.push_back(-arr[i+1]);
            v.push_back(arr[i]);
        }

        ll a = arr[n-2], b = arr[n-1], c = arr[n];

        if(a + b)
        {
            v.push_back(-c);
            v.push_back(-c);
            v.push_back(a+b);
        }
        else if(a + c)
        {
            v.push_back(-b);
            v.push_back(a+c);
            v.push_back(-b);

        }
        else if(b + c)
        {
            v.push_back(b+c);
            v.push_back(-a);
            v.push_back(-a);
        }

    }
    else
    {
        for(int i=1; i<=n-1; i+=2)
        {
            v.push_back(-arr[i+1]);
            v.push_back(arr[i]);
        }
    }

    for(int i=0; i<v.size(); i++)
        cout << v[i] << ' ';
    cout << endl;


int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    t = read();
    while(t--)
        solve();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值