Codeforces Round #619 (Div. 2)B. Motarack's Birthday

本文介绍了一种算法,旨在通过替换数组中的特定元素来最小化数组中相邻元素之间的最大差值。通过收集非缺失元素并计算其边界值的平均数,确定了最佳填充值,从而确保了差值的最小化。

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

B. Motarack’s Birthday
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
Note
In the first test case after replacing all missing elements with 11 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 k, such that the absolute difference between any adjacent element will be ≤0. So, the answer is 1.

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

|a1−a2|=|6−6|=0;
|a2−a3|=|6−9|=3;
|a3−a4|=|9−6|=3;
|a4−a5|=|6−3|=3;
|a5−a6|=|3−6|=3.
So, the maximum difference between any adjacent elements is 3.

题意:在n个数中,把数值为-1的数 都填成数字x,使得k最小 k为相邻两项之间 差的绝对值

思路:可以这样考虑,如果当前位置的数不是-1并且前一位是-1或者后一位是-1,我们就把当前这个数丢进数组sb里,操作完后 -1的取值m就是排序后 sb[0]+sb[sb.size()-1]>>1,为什么这样呢?
因为和-1相邻的数就要和m相减,所以我们就去算sb数组中最大值+最小值的和除以2 保证差值尽可能小
比赛时候写二分 死活过不去。。。就换方法了。。还是太菜

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define me(a,x) memset(a,x,sizeof a)
#define pb(a) push_back(a)
#define pa pair<int,int>
#define fi first
#define se second
int a[100005];
vector<int>sb;
int main()
{

    int t;
    cin>>t;
    while(t--)
    {
      sb.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)) sb.pb(a[i]);
      }
      sort(sb.begin(),sb.end());
      int m;
      if(sb.empty()) m=0;
      else m=(sb[0]+sb[sb.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;
}

后面补了二分做法 比赛时候纯属二分写太搓。。
二分思路
我们二分要代替-1的分的范围,然后去计算w=check(mid) e=check(mid+1)
check 为填入值后 相邻的差的最大值 如果w<e 那么就让 r=mid;
否则就让l=mid;

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define me(a,x) memset(a,x,sizeof a)
#define pb(a) push_back(a)
#define pa pair<int,int>
#define fi first
#define se second
int a[100005];
int n;
int check(int x)
{
    int mi=0;
    for(int i=0; i<n-1; i++)
    {
        if(a[i]==-1 && a[i+1]!=-1)
            mi=max(mi,abs(x-a[i+1]));
        else if(a[i]!=-1 && a[i+1]==-1)
            mi=max(mi,abs(x-a[i]));
        else if(a[i]!=-1 && a[i+1]!=-1)
            mi=max(mi,abs(a[i+1]-a[i]));

    }
    return mi;

}
int main()
{
    int t;cin>>t;
    while(t--)
    {
        cin>>n;
        int ma=-1;
        for(int i=0; i<n; i++)
        {

            cin>>a[i];
            ma=max(ma,a[i]);
        }
        int l=0,r=ma+1,mid;
        while(l+1<r)
        {
            mid=l+r>>1;
            int w=check(mid),e=check(mid+1);
            if(w<e)
                r=mid;
            else
                l=mid;
        }
        int z=check(l),x=check(r);
        if(z<x) cout<<z<<" "<<l<<endl;
        else cout<<x<<" "<<r<<endl;

    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我不会c语言

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

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

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

打赏作者

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

抵扣说明:

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

余额充值