zoukankan      html  css  js  c++  java
  • 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 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 (0k1090≤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 |aiai+1||ai−ai+1| for all 1in11≤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 (1t1041≤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 (2n1052≤n≤105 ) — the size of the array aa .

    The second line of each test case contains nn integers a1,a2,,ana1,a2,…,an (1ai109−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 41054⋅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 (0k1090≤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

    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

    大意是给一个缺少一些数的序列,要求在每一个空缺的位置添上一个相同的数,使得这个序列中相邻两数之差的最大绝对值尽可能小。
    首先把这个序列读入,然后遍历一遍,统计两个量:与空缺位置相邻的数里的最大值mmax和最小值mmin。
    为什么要相邻呢?因为如果一个数不和空缺位置相邻的话,空缺位置不论填什么都和这个数没有关系,最后处理的时候再看它就行了。
    然后令k=(mmax+mmin)/2,再次遍历一遍序列先把空缺位置填上k然后计算相邻两数之差并更新答案。
    #include <bits/stdc++.h>
    using namespace std;
    int a[200005];
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            int n;
            scanf("%d",&n);
            int i;
            memset(a,0,sizeof(a));
            int mmax=-1000000010;
            int mmin=1000000010;
            int cnt_neg=0;
            for(i=1;i<=n;i++)scanf("%d",&a[i]); 
            for(i=1;i<=n;i++)
            {
                //scanf("%d",&a[i]);必须要全读进去再处理,要不然a[i+1]还是0 
                if(a[i]==-1)cnt_neg++;
                if(i>1&&a[i]==-1&&a[i-1]!=-1)
                {
                    mmax=max(mmax,a[i-1]);
                    mmin=min(mmin,a[i-1]);    
                }
                if(i<n&&a[i]==-1&&a[i+1]!=-1)
                {
                    mmax=max(mmax,a[i+1]);
                    mmin=min(mmin,a[i+1]);
                }
            }                
            if(cnt_neg==n)
            {
                cout<<0<<' '<<42<<endl;
                continue;
            }
            int k=(mmax+mmin)/2;
            int m=0;
            for(i=1;i<=n;i++)if(a[i]==-1)a[i]=k; 
            for(i=2;i<=n;i++)
            {
                m=max(m,abs(a[i]-a[i-1])); 
            }
            cout<<m<<' '<<k<<endl;    
        }    
        return 0;
    }


  • 相关阅读:
    webpack高级概念code splitting 和 splitChunks (系列五)
    webpack高级概念Develoment 和 Production 不同环境的配置 (系列四)
    webpack高级概念Tree Shaking (树摇)(系列三)
    HarmonyOS三方件开发指南(16)-VideoCache 视频缓存
    鸿蒙开源第三方组件——uCrop_ohos图片裁剪组件
    Hi3516如何连接Wifi(三)
    【鸿蒙学院】鸿蒙IDE迎来重大更新,新特性足以让你尖叫
    《鸿蒙系统物联网模组——Neptune 三天全攻略》课件、代码
    预览器和编辑器双重发力,DevEco Studio 2.1 Beta 3强势来袭
    强大的鸿蒙开发环境 —— DevEco Studio 2.1 Beta3发布
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/12309382.html
Copyright © 2011-2022 走看看