Codeforces Round #321 (Div. 2)-A. Kefa and First Steps,暴力水过~~
2016-04-30 20:26
537 查看
A. Kefa and First Steps
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Kefa decided to make some money doing business on the Internet for exactly n days. He knows that on the i-th
day (1 ≤ i ≤ n) he makes ai money.
Kefa loves progress, that's why he wants to know the length of the maximum non-decreasing subsegment in sequence ai.
Let us remind you that the subsegment of the sequence is its continuous fragment. A subsegment of numbers is called non-decreasing if all numbers in it follow in the non-decreasing order.
Help Kefa cope with this task!
Input
The first line contains integer n (1 ≤ n ≤ 105).
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
Print a single integer — the length of the maximum non-decreasing subsegment of sequence a.
Examples
input
output
input
output
Note
In the first test the maximum non-decreasing subsegment is the numbers from the third to the fifth one.
In the second test the maximum non-decreasing subsegment is the numbers from the first to the third one.
这个题有点像求非单调递减子序列的长度,但仔细看看题发现是求连续一段非单减子数组的长度,这样和求单调递增子序列的方法很像,但看到数据范围两层循环遍历肯定会超时,题目要求的是子区间非单减的长度,为何要遍历呢?只需判断当前输入的值是否大于等于前一个的值,如果是,则此时的长度就等于前一个长度加一,反之,则此时长度为一,然后继续。看代码:
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Kefa decided to make some money doing business on the Internet for exactly n days. He knows that on the i-th
day (1 ≤ i ≤ n) he makes ai money.
Kefa loves progress, that's why he wants to know the length of the maximum non-decreasing subsegment in sequence ai.
Let us remind you that the subsegment of the sequence is its continuous fragment. A subsegment of numbers is called non-decreasing if all numbers in it follow in the non-decreasing order.
Help Kefa cope with this task!
Input
The first line contains integer n (1 ≤ n ≤ 105).
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
Print a single integer — the length of the maximum non-decreasing subsegment of sequence a.
Examples
input
6 2 2 1 3 4 1
output
3
input
3 2 2 9
output
3
Note
In the first test the maximum non-decreasing subsegment is the numbers from the third to the fifth one.
In the second test the maximum non-decreasing subsegment is the numbers from the first to the third one.
这个题有点像求非单调递减子序列的长度,但仔细看看题发现是求连续一段非单减子数组的长度,这样和求单调递增子序列的方法很像,但看到数据范围两层循环遍历肯定会超时,题目要求的是子区间非单减的长度,为何要遍历呢?只需判断当前输入的值是否大于等于前一个的值,如果是,则此时的长度就等于前一个长度加一,反之,则此时长度为一,然后继续。看代码:
#include<cstdio> #include<cmath> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int N=100000+10; int a ,b ; int main() { int n,i,j; while(~scanf("%d",&n)) { memset(b,0,sizeof(a)); int maxx=0; for(i=1;i<=n;i++) { scanf("%d",&a[i]); b[i]=1; for(j=i;j>=1;j--) { if(a[j]>=a[j-1]&&j!=1) b[i]=b[j-1]+1;//用到了点动归思想; break;//这样就不会超时了,甚至可以简化之; } maxx=max(maxx,b[i]); } printf("%d\n",maxx); } return 0; }
相关文章推荐
- jQuery弹出美女大图片
- 森林大帝讲课录音“深度解析消毒的应用”
- 森林大帝带你“深度解析水质浑浊”
- SweetAlert – 替代 Alert 的漂亮的提示效果
- 59.java编程思想——创建窗口和程序片 Swing
- 二叉树顺序存储的实现
- Java反射
- 58.java编程思想——创建窗口和程序片 一个复杂的Bean
- 57.java编程思想——创建窗口和程序片 可视编程和Beans
- 56.java编程思想——创建窗口和程序片 用户接口API
- Java核心知识点-反射
- 55.java编程思想——创建窗口和程序片 推荐编码方法
- 54.java编程思想——创建窗口和程序片 逻辑与UI分离
- 53.java编程思想——创建窗口和程序片 新型AWT
- 团队之阅读感想
- 52.java编程思想——创建窗口和程序片 程序片限制
- 51.java编程思想——创建窗口和程序片 action替代
- UML学习01——类图
- 50.java编程思想——创建窗口和程序片 布局控制
- 基于WebMagic写的一个csdn博客小爬虫