Codeforces Beta Round #61 (Div. 2) B. Petya and Countryside (DP)
2016-12-05 15:54
2501 查看
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.
Creating artificial rain is an expensive operation. That’s why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture:
As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
Input
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
Output
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
Examples
input
1
2
output
1
input
5
1 2 1 2 1
output
3
input
8
1 2 1 1 1 3 3 4
output
6
题意:
从山上浇水,水往低处流,所以水只能流向高度小于或等于此高度的地方
给你n个数代表山的高度,问最多能浇几个单位
思路:
先从左往右筛一遍,筛出从当前点往左不大于其的最大长度
再从右往左筛一遍,筛出从当前点往右不大于其的最大长度
将每个点的上述两值相加-1便是该点最多能浇的单位数量,找出最大值便可
代码:
memory limit per test256 megabytes
inputstandard input
outputstandard output
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 × n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed height and due to the newest irrigation system we can create artificial rain above each section.
Creating artificial rain is an expensive operation. That’s why we limit ourselves to creating the artificial rain only above one section. At that, the water from each watered section will flow into its neighbouring sections if their height does not exceed the height of the section. That is, for example, the garden can be represented by a 1 × 5 rectangle, where the section heights are equal to 4, 2, 3, 3, 2. Then if we create an artificial rain over any of the sections with the height of 3, the water will flow over all the sections, except the ones with the height of 4. See the illustration of this example at the picture:
As Petya is keen on programming, he decided to find such a section that if we create artificial rain above it, the number of watered sections will be maximal. Help him.
Input
The first line contains a positive integer n (1 ≤ n ≤ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
Output
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
Examples
input
1
2
output
1
input
5
1 2 1 2 1
output
3
input
8
1 2 1 1 1 3 3 4
output
6
题意:
从山上浇水,水往低处流,所以水只能流向高度小于或等于此高度的地方
给你n个数代表山的高度,问最多能浇几个单位
思路:
先从左往右筛一遍,筛出从当前点往左不大于其的最大长度
再从右往左筛一遍,筛出从当前点往右不大于其的最大长度
将每个点的上述两值相加-1便是该点最多能浇的单位数量,找出最大值便可
代码:
/************************************************************************* > File Name: 62B.cpp > Author: SIU > Created Time: 2016年12月05日 星期一 15时28分07秒 ************************************************************************/ #include<cstdio> #include<iostream> #include<cstring> using namespace std; int main() { int n; int num[1050]; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&num[i]); int str1[1050]; int str2[1050]; str1[1]=1; for(int i=2;i<=n;i++) { if(num[i]>=num[i-1]) str1[i]=str1[i-1]+1; else str1[i]=1; } str2 =1; for(int i=n-1;i>0;i--) { if(num[i]>=num[i+1]) str2[i]=str2[i+1]+1; else str2[i]=1; } int max=0; for(int i=1;i<=n;i++) { int item=str1[i]+str2[i]-1; if(item>max) max=item; } printf("%d\n",max); return 0; }
相关文章推荐
- Codeforces Beta Round #6 (Div. 2 Only) D. Lizards and Basements 2 dp
- Codeforces Beta Round #14 (Div. 2) D. Two Paths 树形dp
- Codeforces Beta Round #61 (Div. 2) E. Petya and Post
- Codeforces Round #384 (Div. 2)D. Chloe and pleasant prizes(树DP)
- Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心
- Codeforces Round #293 (Div. 2) D. Ilya and Escalator(概率DP)
- 递推DP Codeforces Round #260 (Div. 1) A. Boredom
- DP Codeforces Round #322 (Div. 2) F. Zublicanes and Mumocrates
- Codeforces Round #267 (Div. 2) C. George and Job(DP)
- Codeforces Round #360 (Div. 2) E The Values You Can Make(DP)
- Codeforces Round #235 (Div. 2) D. Roman and Numbers(状压dp)
- Codeforces Beta Round #50 C. First Digit Law(概率dp,好题)
- Codeforces Round #363 (Div. 2) C. Vacations(DP)
- Codeforces Round #245 (Div. 1) -- B. Working out (DP)
- 【Codeforces Round 326 (Div 2)D】【DP】Duff in Beach 数列重复取数最多k次使得单调不下降
- Codeforces Round #260 (Div. 2)C. Boredom(dp)
- Codeforces Beta Round #6 (Div. 2 Only) A. Triangle 水题
- Codeforces Beta Round #92 (Div. 1 Only)
- Codeforces Round #129 (Div. 1) (各种好dp)
- 【Codeforces Round 331 (Div 2)D】【DP 记忆化搜索 期望DP区间性质 好题】Wilbur and Trees 砍树随机从右从左概率左倒右倒的期望底面覆盖长度