您的位置:首页 > 其它

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

2017-09-26 17:06 399 查看

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
,poUp
,poDown
,neUp
,neDown
;
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
= neDown
= 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐