• Codeforces Round #619 (Div. 2)


    http://codeforces.com/contest/1301

    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
    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
    Note
    In the first test case after replacing all missing elements with 1111 the array becomes [11,10,11,12,11][11,10,11,12,11]. The absolute difference between any adjacent elements is 11. 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 11.

    In the third test case after replacing all missing elements with 66 the array becomes [6,6,9,6,3,6][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,要求用一个数k替换掉所有-1,来实现相邻最大差值最小。输出这个他们。

       解析:比赛时想的是把去掉-1后的最大区间差找出来,思路其实是对的,但是没想到用stl,结果写了一堆bug,WA了好几次。。。。一定要灵活运用stl啊。对于这个题,我们只需要把左或者右出现-1的非-1数找出来,放到vector里(queue不能sort排序),那么k=(vector[0]+vector[size-1])/2即可。因为进入vector的都是需要和m做差的,只要找出vector中最小和最大的中间值,就能保证差值最小了。

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<queue>
    #include<vector>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    typedef long long ll;
    const int maxn=1e5+10;
    ll a[maxn];
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            int n;
            cin>>n;
            for(int i=0;i<n;i++)
            {
                cin>>a[i];
            }
            vector<ll>v;
            for(int i=0;i<n;i++)
            {
                if(a[i]!=-1&&(a[i-1]==-1||a[i+1]==-1))
                {
                    v.push_back(a[i]);
                }
            }
            sort(v.begin(),v.end());
            ll k;
            if(v.empty())  //空,全-1情况
                k=0;
            else
                k=(v[0]+v[v.size()-1])/2;
            ll maxx=-1;
            for(int i=0;i<n;i++)
            {
                if(a[i]==-1)
                {
                    a[i]=k;
                    
                }if(i!=0)
                    {
                        ll mid=fabs(a[i]-a[i-1]);
                        //cout<<mid<<endl;
                        if(mid>maxx)
                        maxx=mid;
                    }
            }
            cout<<maxx<<" "<<k<<endl;
        }
    }

    Ayoub's function CodeForces - 1301C(组合数学)

    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

    Input
    5
    3 1
    3 2
    3 3
    4 0
    5 2
    Output
    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.

        题意就不说了。很好理解。主要是题解了,借用某老哥一句话,这种题,会的话就是A题难度,否则就是F题难度。我做的时候是怎么也没想到要用排列组合这个思想,自己实在是太菜了!

    反向思考,1怎么摆不好求,但是我们可以只看0的摆放,因为要想实现结果最大,各个1周围的0应该平均地放。n个位置,m个1,那么有m+1个位置来放0,那么有:

          cnt1=(n-m)/(m+1),表示平均下来至少每个位置放cnt1个。

          cnt2=(n-m)%(m+1),如果除不尽,那么有cnt2个位置放cnt1+1个。

          all=(n+1)*n/2,均使用此公式,表示长度n有多少个子集

              m+1-cnt2个位置放cnt1个,cnt2个位置放cnt1+1个

          总的个数就是:all-(cnt1+1)*cnt1/2*(m+1-cnt2)-(cnt1+1+1)*(cnt1+1)/2*cnt2

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int maxn = 1e3+5;
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            ll n,m;
            cin>>n>>m;
            ll all=(n+1)*n/2;
            ll cnt1=(n-m)/(m+1);
            ll cnt2=(n-m)%(m+1);
            cout<<all-(cnt1+1)*cnt1/2*(m+1-cnt2)-(cnt1+1+1)*(cnt1+1)/2*cnt2<<endl;
        }
    }
  • 相关阅读:
    2018年全国多校算法寒假训练营练习比赛(第四场)
    STL中的map
    java异常处理
    过滤器与监听器原理详解
    cookie和session机制区别
    servlet运行原理
    $.ajax相关用法
    jdk源码库
    Tomcat 系统架构与设计模式,第 1 部分: 工作原理
    Tomcat源码分析(二)------ 一次完整请求的里里外外
  • 原文地址:https://www.cnblogs.com/liyexin/p/12308485.html
Copyright © 2020-2023  润新知