Problem D. Dales and Hills - Gym - 101411D 【动态规划经典题 - 思维】

Problem D. Dales and Hills

vj题目链接

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.

Examples

input

2
10
4 4 1 6 3 2 1 2 5 7
10
2 3 4 5 6 7 8 9 10 9

output

1 3
1 0

题意:给定一个数列 a,定义“hill”为一个区间[i,k]中有一点j,当在区间[i,j)中任意一点t,满足a[t] < a[t+1],在区间[j,k)中,满足a[t] < a[t+1]; 在此基础上定义“height of hill”为min{j-i,k-j};同理定义“dale”,当区间[i,j)中任意一点t,满足a[t] >a[t+1],在区间(j,k]中,满足a[t] > a[t+1],在此基础上定义“depth of the dale”为min{j-i,k-j}; 最后输出所谓的 height of hill , depth of the dale
分析:读题后就能发现这是道动态规划的题,定义四个数组,分别表示
poUp[i] 在正方向上第i个数之前有多少递增的数
poDown[i] 在正方向上第i个数之前有多少递减的数
neUp[i] 在负方向上第i个数之前有多少递增的数
neDown[i] 在负方向上第i个数之前有多少递减的数
很显然最后答案就是 枚举min(poUp[i],neUp[i])的最大值和枚举min(neDown[i],neDown[i])的最大值(有点拗口,不过看代码就顺眼了)
坑点: 输入文件啊,坑死了我

参考代码

#include<bits/stdc++.h>
using namespace std;

const int N = 1e6 + 10;
inline int read()  //字符输入可以加快读入速度
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int a[N],poUp[N],poDown[N],neUp[N],neDown[N];
int main(){
    freopen("dales.in","r",stdin);
    freopen("dales.out","w",stdout);
    ios_base::sync_with_stdio(0);
    int T;T = read();
    while(T--){
        int n;n = read();
        for(int i = 1;i <= n;i++) a[i] = read();
        poUp[1] = poDown[1] = 0;
        for(int i = 2;i <= n;i++){
            if(a[i] > a[i-1]) poUp[i] = poUp[i-1] + 1;
            else poUp[i] = 0;
            if(a[i] < a[i-1]) poDown[i] = poDown[i-1]+1;
            else poDown[i] = 0;
        }
        neUp[n] = neDown[n] = 0;
        for(int i = n-1;i >= 1;i--){
            if(a[i] > a[i+1]) neUp[i] = neUp[i+1] + 1;
            else neUp[i] = 0;
            if(a[i] < a[i+1]) neDown[i] = neDown[i+1] + 1;
            else neDown[i] = 0;
        }
        int ans1 = -1,ans2 = -1;
        for(int i = 1;i <= n;i++){
            ans1 = max(ans1,min(poUp[i],neUp[i]));
            ans2 = max(ans2,min(poDown[i],neDown[i]));
        }
        cout<<ans1<<' '<<ans2<<endl;
    }
    return 0;
}
  • 如有错误或遗漏,请私聊下UP,ths
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值