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;
    }


  • 相关阅读:
    LAMP LNMP 和 LNMPA
    nginx版本如何选择?
    如何看apache的版本号
    CLR Via CSharp读书笔记(12):泛型
    要先怀疑外部代码的错误,再检测是不是自己代码的问题
    redis的那些事
    An Illustrated Guide to SSH Agent Forwarding
    Using sshagent with ssh
    dtach
    [原创]bind DNS IP列表的精确获取
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/12309382.html
Copyright © 2011-2022 走看看