Codeforces Round #321 (Div. 2)-A. Kefa and First Steps,暴力水过~~
2 seconds
256 megabytes
standard input
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!
The first line contains integer n (1 ≤ n ≤ 105).
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Print a single integer — the length of the maximum non-decreasing subsegment of sequence a.
6
2 2 1 3 4 1
3
3
2 2 9
3
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[N],b[N];
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;
}
最新文章
- TCP流量控制与拥塞控制
- iOS中的__typeof与typeof
- POJ3635 Full Tank?(DP + Dijkstra)
- 设置h5页面不可复制文字
- SQL Server Schema
- GitHub详解(转)
- PHP微信支付开发之扫描支付(模式二)后如何回调
- Oracle的正则函数之regexp_like
- Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.commons.EmptyVisitor
- 用js来实现那些数据结构14(树02-AVL树)
- jQueryUI中Datepicker(日历)插件使用
- OpenStack 部署步骤详解(mitaka/ocata/一键部署)
- git push 每次都要输入用户名密码
- Flutter - Error: &#39;xxx&#39; is imported from both package...
- HDU2883_kebab
- hdoj1879 继续畅通工程(Prime || Kruskal)
- JQuery包装集size,length,index,slice,find,filter,is,children,next,nextAll,parent,parents,closest,siblings,add,end,andSelf的用法
- PHP中类型约束
- 2015 DevOps状态调查报告
- 题解【luoguP3644 [APIO2015]八邻旁之桥】