Codeforces Round #619 (Div. 2)

A. Three Strings

You are given three strings aa, bb and cc of the same length nn. The strings consist of lowercase English letters only. The ii-th letter of aa is aiai, the ii-th letter of bb is bibi, the ii-th letter of cc is cici.

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

For example, if aa is “code”, bb is “true”, and cc is “help”, you can make cc equal to “crue” taking the 11-st and the 44-th letters from aa and the others from bb. In this way aa becomes “hodp” and bb becomes “tele”.

Is it possible that after these swaps the string aa becomes exactly the same as the string bb?

Input
The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1001≤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 aa.

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

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

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

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

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

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

Example

inputCopy
4
aaa
bbb
ccc
abc
bca
bca
aabb
bbaa
baba
imi
mii
iim
outputCopy
NO
YES
YES
NO
Note
In the first test case, it is impossible to do the swaps so that string aa becomes exactly the same as string bb.

In the second test case, you should swap cici with aiai for all possible ii. After the swaps aa becomes “bca”, bb becomes “bca” and cc becomes “abc”. Here the strings aa and bb are equal.

In the third test case, you should swap c1c1 with a1a1, c2c2 with b2b2, c3c3 with b3b3 and c4c4 with a4a4. Then string aa becomes “baba”, string bb becomes “baba” and string cc becomes “abab”. Here the strings aa and bb are equal.

In the fourth test case, it is impossible to do the swaps so that string aa becomes exactly the same as string bb.

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main()
{
    int n;
    cin>>n;
    while(n--){
        char a[101]={0},b[101]={0},c[101]={0};
        scanf("%s%s%s",a,b,c);
        int l=strlen(a),flag=0;
        for(int i=0;i<l;i++){
            if(a[i]!=c[i]&&b[i]!=c[i]){
                printf("NO\n");
                flag=0;
                break;
            }
            else{
                flag=1;
            }
        }
        if(flag==1){
            printf("YES\n");
        }
    }
    return 0;
}

B. Motarack’s Birthday

Dark is going to attend Motarack’s birthday. Dark decided that the gift he is going to give to Motarack is an array aa of nn non-negative integers.

Dark created that array 10001000 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 kk (0≤k≤1090≤k≤109) and replaces all missing elements in the array aa with kk.

Let mm be the maximum absolute difference between all adjacent elements (i.e. the maximum value of |ai−ai+1||ai−ai+1| for all 1≤i≤n−11≤i≤n−1) in the array aa after Dark replaces all missing elements with kk.

Dark should choose an integer kk so that mm is minimized. Can you help him?

Input
The input consists of multiple test cases. 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 one integer nn (2≤n≤1052≤n≤105) — the size of the array aa.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (−1≤ai≤109−1≤ai≤109). If ai=−1ai=−1, then the ii-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 nn for all test cases does not exceed 4⋅1054⋅105.

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

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

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

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

Example

inputCopy
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
outputCopy
1 11
5 35
3 6
0 42
0 0
1 2
3 4
Note
In the first test case after replacing all missing elements with 1111 the array becomes [11,10,11,12,11]. The absolute difference between any adjacent elements is 1. It is impossible to choose a value of kk, such that the absolute difference between any adjacent element will be ≤0≤0. So, the answer is 1.

In the third test case after replacing all missing elements with 66 the array becomes [6,6,9,6,3,6].

|a1−a2|=|6−6|=0|a1−a2|=|6−6|=0;
|a2−a3|=|6−9|=3|a2−a3|=|6−9|=3;
|a3−a4|=|9−6|=3|a3−a4|=|9−6|=3;
|a4−a5|=|6−3|=3|a4−a5|=|6−3|=3;
|a5−a6|=|3−6|=3|a5−a6|=|3−6|=3.
So, the maximum difference between any adjacent elements is 33.
题目大意:给出一组数,其中部分丢失,丢失数用-1表示,问在丢失位置上补上什么数,可以使相邻数差值最小

解题思路:
当数列为-1,n1,-1,n2,n3,-1,n4,-1时,不难看出只有在-1位置填入n1,n2,n3,n4中最大值和最小值的中位数,才可以使相邻数差值最小

#include<iostream>
#include <cstdio>
#include<algorithm>
#include <math.h>
using namespace std;
const int N=1e5+1000;
int a[N];
int t;
int n;
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		int maxn=0;
		int minn=1e9;
		a[0]=-1;a[n+1]=-1;
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		for(int i=1;i<=n;i++)
		{
			if(a[i]==-1)
			{
				if(a[i-1]!=-1)
				{
					maxn=max(maxn,a[i-1]);
					minn=min(minn,a[i-1]);
				}
				if(a[i+1]!=-1)
				{
					maxn=max(maxn,a[i+1]);
					minn=min(minn,a[i+1]);
				}
			}
		}
			int p=(maxn+minn)/2;
			int ans=0;
			for(int i=1;i<=n-1;i++)
			{
				if(a[i]==-1) a[i]=p;
				if(a[i+1]==-1) a[i+1]=p;
				ans=max(ans,abs(a[i]-a[i+1]));
			}
		 cout<<ans<<" "<<p<<endl;
	}
}

C. Ayoub’s function

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

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

For example, if s=s=“01010” then f(s)=12f(s)=12, because there are 1212 such pairs (l,r)(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)(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 nn and mm and asked him this problem. For all binary strings ss of length nn which contains exactly mm symbols equal to “1”, find the maximum value of f(s)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 tt (1≤t≤1051≤t≤105) — the number of test cases. The description of the test cases follows.

The only line for each test case contains two integers nn, mm (1≤n≤1091≤n≤109, 0≤m≤n0≤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)f(s) over all strings ss of length nn, which has exactly mm symbols, equal to “1”.

Example

inputCopy
5
3 1
3 2
3 3
4 0
5 2
outputCopy
4
5
6
0
12
Note
In the first test case, there exists only 33 strings of length 33, which has exactly 11 symbol, equal to “1”. These strings are: s1=s1=“100”, s2=s2=“010”, s3=s3=“001”. The values of ff for them are: f(s1)=3,f(s2)=4,f(s3)=3f(s1)=3,f(s2)=4,f(s3)=3, so the maximum value is 44 and the answer is 44.

In the second test case, the string ss with the maximum value is “101”.

In the third test case, the string ss with the maximum value is “111”.

In the fourth test case, the only string ss of length 44, which has exactly 00 symbols, equal to “1” is “0000” and the value of ff for that string is 00, so the answer is 00.

In the fifth test case, the string ss with the maximum value is “01010” and it is described as an example in the problem statement.
题目大意:给了一个长度为n的01串,其中有m个1,现在让你构造一个01串s,让他的f(s)最大,f(s)为含有1的子串个数
解题思路:
用总的子串个数n*(n+1)/2j减去只含有0的子串个数,不妨将所有的0 (n-m个),分为m+1组在每组中间加入一个1,如0101010,得num=(n-m)/m+1,对于r=(n-m)%(m+1)则在m+1份里每一份放一个
得到公式:ans= n*(n+1)/2-r*(num+1)(m+1-r)(num+2)/2-num(num+1)/2

#include<iostream>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long  n,m;
        cin>>n>>m;
        long long  sum=n*(n+1)/2;
        long long  num=(n-m)/(m+1);
        long long  r=(n-m)%(m+1);
        cout<<sum-r*(num+1)*(num+2)/2-(m+1-r)*num*(num+1)/2<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向岸看

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值