Codeforces Round #619 (Div. 2)

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 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.

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

题意:在n个数中,把数值为-1的数 都填成数字x,使得k最小 k为相邻两项之间 差的绝对值
可以这样考虑,如果当前位置的数不是-1并且前一位是-1或者后一位是-1,我们就把当前这个数丢进数组s里,m的取值就是排序后 s[0]+s[s.size()-1]/2,为什么这样呢?
因为和-1相邻的数就要和m相减,所以我们就去算sb数组中最大值+最小值的和除以2 保证差值尽可能小

#include<bits/stdc++.h>
using namespace std;

int a[100005];
vector<int>s;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
      s.clear();
      int n;cin>>n;
      for(int i=1;i<=n;i++) cin>>a[i];
      for(int i=1;i<=n;i++){
         if(a[i]!=-1 && (a[i-1]==-1 || a[i+1]==-1)) s.push_back(a[i]);
      }
      sort(s.begin(),s.end());
      int m;
      if(s.empty()) m=0;
      else m=(s[0]+s[s.size()-1])/2;
      int k=0;
      for(int i=1;i<=n;i++){
          if(a[i]==-1) a[i]=m;
          if(i>=2) k=max(k,abs(a[i]-a[i-1]));
      }
      cout<<k<<" "<<m<<endl;

    }
    return 0;
}

C.

Ayoub’s function

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”.

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 3 strings of length 3, which has exactly 1 symbol, equal to “1”. These strings are: s1=“100”, s2=“010”, s3=“001”. The values of f for them are: f(s1)=3,f(s2)=4,f(s3)=3, so the maximum value is 4 and the answer is 4.

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

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

In the fourth test case, the only string s of length 4, which has exactly 0 symbols, equal to “1” is “0000” and the value of f for that string is 0, so the answer is 0.

In the fifth test case, the string s with the maximum value is “01010” and it is described as an example in the problem statement.

简单来说就是划分0,1将0等分成k个区间,然后要用到容斥的思想我们先求出最大可能值,只需要减去不可能的情况就可

#include<bits/stdc++.h>
using namespace std;

int T;
long long n,m,len,num1,num2,ans;

int main(){
cin>>T;
while(T–){
cin>>n>>m;
len=(n-m)/(m+1);
num1=(n-m-len*(m+1));
num2=m+1-num1;
ans=n*(n-1)-len*(len+1)num1-len(len-1)*num2;
ans/=2;
cout<<ans+m<<endl;
}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值