Codeforces Round #619 (Div. 2)(ABC) 2020.02.14

A

题目

You are given three strings a, b and c of the same length n. The strings consist of lowercase English letters only. The i-th letter of a is ai, the i-th letter of b is bi, the i-th letter of c is ci.

For every i (1≤i≤n) you must swap (i.e. exchange) ci with either ai or bi. So in total you’ll perform exactly n swap operations, each of them either ci↔ai or ci↔bi (i iterates over all integers between 1 and n, inclusive).

For example, if a is “code”, b is “true”, and c is “help”, you can make c equal to “crue” taking the 1-st and the 4-th letters from a and the others from b. In this way a becomes “hodp” and b becomes “tele”.

Is it possible that after these swaps the string a becomes exactly the same as the string b?

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

The first line of each test case contains a string of lowercase English letters a.

The second line of each test case contains a string of lowercase English letters b.

The third line of each test case contains a string of lowercase English letters c.

It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding 100.

Output
Print t lines with answers for all test cases. For each test case:

If it is possible to make string a equal to string b print “YES” (without quotes), otherwise print “NO” (without quotes).

You can print either lowercase or uppercase letters in the answers.

翻译

你有三个相同长度的字符串a, b和c。这些字符串由小写的英文字母组成。a的第i个字母是ai, b的第i个字母是bi, c的第i个字母是ci。
对于每个i(1≤i≤n),您必须与ai或bi交换(即交换)ci。所以在总您将执行完全n互换操作,它们中的每一个ci↔ai或ci↔bi(我遍历所有整数1到n,包容)。
例如,如果a是“code”,b是“true”,c是“help”,你可以让c等于“crue”,取a的第1-st个字母和第4个字母,其余的是b的,这样a就变成了“hodp”,b变成了“tele”。
有没有可能在这些交换之后,字符串a变得和字符串b完全一样?

输入
输入由多个测试用例组成。第一行包含一个整数t(1≤t≤100)——测试用例的数量。测试用例的描述如下。
每个测试用例的第一行包含一个小写英文字母a字符串。
每个测试用例的第二行包含一个小写英文字母b的字符串。
每个测试用例的第三行包含一个小写英文字母c的字符串。
保证在每个测试用例中,这三个字符串都是非空的,并且具有相同的长度,长度不超过100。

输出
打印所有测试用例的答案。对于每个测试用例:
如果可以使字符串a与字符串b相等,则打印“YES”(不带引号),否则打印“NO”(不带引号)。
您可以在答案中打印小写或大写字母。

例子

input
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim

output
NO
YES
YES
NO

思路

只要a[i],b[i]中的一个和c[i]相等,直接把另一个和c[i]换就好了。

代码

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int n;
    cin >> n;
    while(n--)
    {
        string str,bbb,ccc;
        int i;
        cin >> str >> bbb >> ccc;

        int f=1;
        for(i=0;i<str.length();i++)
        {
            if(str[i]==ccc[i] || bbb[i]==ccc[i])
            {
                continue;
            }
            else
            {
                f=0;
                break;
            }
        }

        if(f==1)
        {
            cout << "YES" <<endl;
        }
        if(f==0)
        {
            cout << "NO" <<endl;
        }
    }
    return 0;
}

B

题目

Dark is going to attend Motarack’s birthday. Dark decided that the gift he is going to give to Motarack is an array a of n non-negative integers. Dark created that array 1000 years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn’t have much time so he wants to choose an integer k (0 k 109) and replaces all missing elements in the array a with k. Let m be the maximum absolute difference between all adjacent elements (i.e. the maximum value of |ai ai+1| for all 1 i n 1) in the array a after Dark replaces all missing elements with k. Dark should choose an integer k so that m is minimized. Can you help him.

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

The first line of each test case contains one integer n (2≤n≤105) — the size of the array a.

The second line of each test case contains n integers a1,a2,…,an (−1≤ai≤109). If ai=−1, then the i-th integer is missing. It is guaranteed that at least one integer is missing in every test case.

It is guaranteed, that the sum of n for all test cases does not exceed 4⋅105.

Output
Print the answers for each test case in the following format:

You should print two integers, the minimum possible value of m and an integer k (0≤k≤109) that makes the maximum absolute difference between adjacent elements in the array a equal to m.

Make sure that after replacing all the missing elements with k, the maximum absolute difference between adjacent elements becomes m.

If there is more than one possible k, you can print any of them.

翻译

黑暗将要参加Motarack的生日。Dark决定给Motarack的礼物是一个数组a,包含n个非负整数。黑暗1000年前创造了那个数组,所以那个数组中的一些元素消失了。Dark知道Motarack不喜欢看到一个数组中有两个相邻的元素,而且它们之间的绝对差异很大。他没有太多的时间所以他想选择一个整数k (0 k 109)和替换所有缺失的元素数组中k。让米是所有相邻元素之间的最大绝对差(即|哀哀的最大值为所有1我n + 1 | 1)数组中的一个天黑后替换所有缺失的元素k。黑暗应该选择一个整数k m是最小化。你能帮助他吗?

输入

输入由多个测试用例组成。第一行包含一个整数t(1≤t≤104)——测试用例的数量。测试用例的描述如下。
每个测试用例的第一行包含一个整数n(2≤n≤105)——数组a的大小。
每个测试用例的第二行包含n个整数a1,a2,…,an(−1≤ai≤109)。如果ai= - 1,则第i个整数丢失。保证每个测试用例中至少缺少一个整数。
它保证,n的总和为所有测试用例不超过4⋅105。

输出

用以下格式打印每个测试用例的答案:
您应该打印两个整数,m的最小可能值和一个整数k(0≤k≤109),使数组中相邻元素之间的最大绝对差等于m。
确保用k替换所有缺失的元素后,相邻元素之间的最大绝对差为m。
如果有不止一个k,你可以打印任何一个。

例子

input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5

output
1 11
5 35
3 6
0 42
0 0
1 2
3 4

大意

思路

来自于一位大佬的思路
记录所有与-1接壤的数字,排序后取中值即可保证填入数字后其差值最小;再用所有相邻非空数的差值来初始化最小初值,两者取较大输出即可。注意long long。

代码

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define INF 0x3f3f3f3f

ll a[100005];
ll b[100005];

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int num=0;//统计非-1出现个数
        for(int i=1; i<=n; i++)
        {
            cin>>a[i];
            if(a[i]!=-1)
                num++;
        }
        if(num==0) //若全为-1输出0,0
        {
            cout<<0<<' '<<0<<endl;
            continue;
        }
        ll maxs=-1;
        ll mins=INF;
        for(int i=1; i<=n; i++)//找最大值,最小值
        {
            if(a[i]==-1)
            {
                if(i-1>=1&&a[i-1]!=-1)
                {
                    mins=min(mins,a[i-1]);
                    maxs=max(maxs,a[i-1]);
                }
                if(i+1<=n&&a[i+1]!=-1)
                {
                    mins=min(mins,a[i+1]);
                    maxs=max(maxs,a[i+1]);
                }
            }
        }
        ll k=(maxs+mins)/2;//找中间值
        
        if(a[1]==-1)
            a[1]=k;
        ll maxss=-1;
        for(int i=2; i<=n; i++)
        {
            if(a[i]==-1)
                a[i]=k;
            maxss=max(maxss,abs(a[i]-a[i-1]));//找出最大差值
        }
        cout<<maxss<<' '<<k<<endl;
    }
    return 0;
}

C

题目

Ayoub thinks that he is a very smart person, so he created a function f(s), where s is a binary string (a string which contains only symbols “0” and “1”). The function f(s) is equal to the number of substrings in the string s that contains at least one symbol, that is equal to “1”.

More formally, f(s) is equal to the number of pairs of integers (l,r), such that 1≤l≤r≤|s| (where |s| is equal to the length of string s), such that at least one of the symbols sl,sl+1,…,sr is equal to “1”.

For example, if s=“01010” then f(s)=12, because there are 12 such pairs (l,r): (1,2),(1,3),(1,4),(1,5),(2,2),(2,3),(2,4),(2,5),(3,4),(3,5),(4,4),(4,5).

Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers n and m and asked him this problem. For all binary strings s of length n which contains exactly m symbols equal to “1”, find the maximum value of f(s).

Mahmoud couldn’t solve the problem so he asked you for help. Can you help him?

Input

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

The only line for each test case contains two integers n, m (1≤n≤109, 0≤m≤n) — the length of the string and the number of symbols equal to “1” in it.

Output

For every test case print one integer number — the maximum value of f(s) over all strings s of length n, which has exactly m symbols, equal to “1”.

翻译

Ayoub认为他是一个非常聪明的人,所以他创建了一个函数f(s),其中s是一个二进制字符串(一个只包含符号“0”和“1”的字符串)。函数f(s)等于字符串s中包含至少一个符号的子字符串的数量,即等于“1”。
更正式地说,f(s)等于整数对的数目(l,r),使1≤l≤r≤|s|(其中|s|等于字符串s的长度),使至少一个符号sl,sl+1,…,sr等于“1”。
例如,如果s = " 01010 " f (s) = 12,因为有12双(左,右):(1、2),(1、3),(4),(1、5),(2,2),(2、3),(2、4)(2、5),(3、4),(3、5)(4,4),(4、5)。
Ayoub也认为他比Mahmoud聪明,所以他给了他两个整数n和m,然后问他这个问题。对于所有长度为n的二进制字符串s,其中包含恰好等于“1”的m个符号,求f(s)的最大值。
麦哈麦得解决不了这个问题,所以他找你帮忙。你能帮助他吗?

输入

输入由多个测试用例组成。第一行包含一个整数t(1≤t≤105)——测试用例的数量。测试用例的描述如下。
每个测试用例的唯一行包含两个整数n, m(1≤n≤109,0≤m≤n)——字符串的长度和其中等于“1”的符号数量。

输出

对于每个测试用例,打印一个整数——f(s)在所有长度为n的字符串s上的最大值,它正好有m个符号,等于“1”。

例子

input
5
3 1
3 2
3 3
4 0
5 2

output
4
5
6
0
12

大意

思路

逆向考虑,用总的方案数减去只有0的方案数即可,相当于将n-m个0分为m+1块,让所有块的方案数之和最小。也是一个公式,结合递归。

代码

思路来源于这位大佬

#include<bits/stdc++.h>

using namespace std;

#define ll long long
int t;
ll n,m;

inline ll cal(ll x)
{
    return x * (x+1) / 2;
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld %lld",&n,&m);
        n -= m;
        ll k = n/(m+1),r = n % (m+1);
        ll ans = cal(n+m) - cal(k+1) * r - cal(k) *(m+1-r);
        printf("%lld\n",ans);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值