zoukankan      html  css  js  c++  java
  • Dales and Hills Gym

    Problem D. Dales and Hills
    Input le: dales.in
    Output le: dales.out
    Time limit: 2 seconds
    Memory limit: 256 megabytes
    Let's consider a number sequence a1; · · · ; aN . We call the continuous subsequence ai
    ; · · · ; aj ; · · · ; ak
    (1 ≤ i < j < k ≤ N) of the sequence a hill if at < at+1 for any i ≤ t < j and at > at+1 for any j ≤ t < k.
    In this case we call min{j − i; k − j} the height of the hill. Similarly, we call the continuous subsequence
    a dale if at > at+1 for any i ≤ t < j and at < at+1 for any j ≤ t < k. In this case we call min{j −i; k −j}
    the depth of the dale.
    Compute the height of the highest hill and the depth of the deepest dale in the given sequence.
    Input
    The rst line of the input le contains T (1 ≤ T ≤ 100 000), the number of test cases. The test cases
    follow, occupying two lines each. The rst of the two lines contains N (1 ≤ N ≤ 1 000 000), the second
    the members of the sequence, separated by spaces. The sum of values of N over all test cases in the le
    does not exceed 1 000 000. The absolute values of the members of the sequences do not exceed 100 000.
    Output
    The output le should consist of T lines and each line should contain two integers, the height of the highest
    hill and the depth of the deepest dale. If there are no hills or no dales, output 0 in the corresponding
    position.
    Example
    dales.in dales.out
    2
    10
    4 4 1 6 3 2 1 2 5 7
    10
    2 3 4 5 6 7 8 9 10 9
    1 3
    1 0

    题目大意:给出一个序列,求最高的山和最深的山谷。如果中间没有平的(a[i]==a[j]),那么必然是连续的先减后增或者先增后减。如果中间有平的地方,就重新判断是先增后减还是先减后增。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #define maxn 1000005
    using namespace std;
    int a[maxn];
    int h1,h2,h,d,d1,d2,mh,md,n,i,j,flag;//h1,h2,是山左右两边的深度,d1,d2是山谷左右两边的深度
    void dale()
    {
        while(a[j-1]>a[j]&&j<=n) j++;
         d1=j-i-1; 
         if(flag)
         {
             h2=j-i-1;
             h=min(h1,h2);
             mh=max(mh,h);
         }
         i=j-1;
         if(a[i]==a[j]) return ;
         while(a[j-1]<a[j]&&j<=n) j++;
             d2=h1=j-i-1; 
             i=j-1;
             d=min(d1,d2);
             md=max(md,d);
             flag=1;
    
    }
    void hill()
    {
        while(a[j-1]<a[j]&&j<=n) j++;
        h1=j-i-1;
        if(flag)
        {
            d2=j-i-1;
            d=min(d1,d2);
            md=max(md,d);
        }
        i=j-1;
        if(a[i]==a[j]) return ;
        while(a[j-1]>a[j]&&j<=n) j++;
        h2=d1=j-i-1; 
        i=j-1;
        h=min(h1,h2);
        mh=max(mh,h);
        flag=1;
    }
    int main()
    {
         //freopen("dales.in", "r", stdin);
       // freopen("dales.out", "w", stdout);
        int t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            for(int k=1;k<=n;k++)
                scanf("%d",&a[k]);
            h1=h2=h=d1=d2=d=mh=md=0;
            i=1;j=2;
            flag=0;
            while(j<=n)
            {
                if(a[i]==a[j])
                {
                    i++;
                    j++;
                    flag=0;
                }
                if(a[i]>a[j]) dale();
                if(a[i]<a[j]) hill();
            }
            printf("%d %d
    ",mh,md);
        }
        return 0;
    }
  • 相关阅读:
    职业生涯规划
    Java中double函数,四舍五入并保留小数点后两位的4种方法,BMI案例
    获取request.getSession().setAttribute()的值【详解加案例】
    Mac 如何安装Homebrew?
    XCode6的iOS Simulator 文件保存位置
    iOS8无法弹出本地通知?
    Xcode如何找到默认的生成路径?
    Android 实现简单音乐播放器(二)
    Android 实现简单音乐播放器(一)
    Android 如何实现带滚动条的TextView,在更新文字时自动滚动到最后一行?
  • 原文地址:https://www.cnblogs.com/Twsc/p/7284493.html
Copyright © 2011-2022 走看看