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 casesfollow, 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 highesthill and the depth of the deepest dale. If there are no hills or no dales, output 0 in the corresponding
position.
Examples
input
210
4 4 1 6 3 2 1 2 5 7
10
2 3 4 5 6 7 8 9 10 9
output
1 31 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
相关文章推荐
- hihocoder#1529 : 不上升序列&&Codeforces-713C:Sonya and Problem Wihtout a Legend(思维)
- hdu 5284 wyh2000 and a string problem(没有算法,只考思维,字符数组得开20万,不然太小了)
- codeforces742 B. Arpa’s obvious problem and Mehrdad’s terrible solution 【思维 + 亦或】
- 动态规划 (Dynamic Programming) 之 背包问题合辑 (Knapsack, Subset Sum, Partition and change making problem )
- hdu 5284 wyh2000 and a string problem(没有算法,仅仅考思维,字符数组得开20万,不然太小了)
- I - Por Costel and the Pairs Gym - 100923I _思维啊——可惜我现在还没
- HDU 5654 Clarke and problem(动态规划)
- Problem B. Sherlock and Watson Gym Secrets Google APAC 2017 University Test Round B
- D. Dasha and Very Difficult Problem----思维题
- Codeforces Round #410 (Div. 2) Mike and gcd problem 思维
- codeforces 877 problem B Nikita and string 【思维 + 前后缀思想】
- Round B APAC Test 2017 Problem B. Sherlock and Watson Gym Secrets
- Codeforces 713C C. Sonya and Problem Wihtout a Legend (经典DP)
- HDU:1069 Monkey and Banana(经典动态规划DP)
- UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
- J - Pashmak and Parmida's problem CodeForces - 459D(思维优化+树状数组)
- 状态压缩DP poj—1185 炮兵阵地 和 cf 111 Problem C Petya and Spiders (经典)
- CF#798 C. Mike and gcd problem(思维)
- GYM 101149 L. Right Build【最短路+思维】经典题
- Problem B. Black and White Gym-100801B] 想象力