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;
}
Codeforces Round #321 (Div. 2)-A. Kefa and First Steps,暴力水过~~的更多相关文章
- Codeforces Round #321 (Div. 2) A. Kefa and First Steps 水题
A. Kefa and First Steps Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/58 ...
- Codeforces Round #321 (Div. 2) A. Kefa and First Steps【暴力/dp/最长不递减子序列】
A. Kefa and First Steps time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- codeforces水题100道 第十四题 Codeforces Round #321 (Div. 2) A. Kefa and First Steps (brute force)
题目链接:http://www.codeforces.com/problemset/problem/580/A题意:求最长连续非降子序列的长度.C++代码: #include <iostream ...
- Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash
E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...
- Codeforces Round #321 (Div. 2) C. Kefa and Park dfs
C. Kefa and Park Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/probl ...
- Codeforces Round #321 (Div. 2) B. Kefa and Company 二分
B. Kefa and Company Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/pr ...
- Codeforces Round #321 (Div. 2) D. Kefa and Dishes 状压dp
题目链接: 题目 D. Kefa and Dishes time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 W ...
- Codeforces Round #321 (Div. 2) D. Kefa and Dishes(状压dp)
http://codeforces.com/contest/580/problem/D 题意: 有个人去餐厅吃饭,现在有n个菜,但是他只需要m个菜,每个菜只吃一份,每份菜都有一个欢乐值.除此之外,还有 ...
- Codeforces Round #321 (Div. 2) E Kefa and Watch (线段树维护Hash)
E. Kefa and Watch time limit per test 1 second memory limit per test 256 megabytes input standard in ...
随机推荐
- 生产环境中nginx既做web服务又做反向代理
一.写对于初入博客园的感想 众所周知,nginx是一个高性能的HTTP和反向代理服务器,在以前工作中要么实现http要么做反向代理或者负载均衡.尚未在同一台nginx或者集群上同时既实现HTTP又实现 ...
- Nagios安装与部署
Nagios概述: Nagios是一款开源免费(也有收费版的Nagios XI)的监控工具,可以用以监控Windows.Linux.Unix.Router.Switch,可以监控指定主机的物理基础资源 ...
- Vue nextTick 理解
官网解释: 将回调延迟到下次 DOM 更新循环之后执行.在修改数据之后立即使用它,然后等待 DOM 更新.它跟全局方法 Vue.nextTick 一样,不同的是回调的 this 自动绑定到调用它的实例 ...
- 让搜狗输入法更符合编程/vim使用的配置
1. “菜单”—“设置属性”—“常用”—“初始状态”里的“中/英文”选项,选中“英文” 2. 设置属性里的“高级”里的“高级模式”,点“英文输入法设置”,“启动时启用英文输入法”选中 3. 按键-中英 ...
- mySQL 从删库到跑路
问题: 使用python实现load data infile ...向mySQL中导入数据.虽然成功执行但是数据库中没增加记录. 解决: zz的我execute之后没有commit.
- 微信小程序 图片加载失败处理方案
小程序端展示网络资源图片可能会失败,下面介绍一种自己的处理方法 1. js文件中判断图片 url 是否存在,存在则正常显示,不存在则替换url为本地默认图片 2. 当图片 url 存在,但是加载失败时 ...
- 搜索模板elasticsearch
搜索: like 对中文分词效率与支持都不太友好elasticsearch 实时的(效率高).分布式(可扩展)的搜索和分析引擎,基于Lucene全文搜索引擎工具包,算法基于倒排索引算法(eg:一篇文章 ...
- 四次元新浪微博客户端Android源码
四次元新浪微博客户端Android源码 源码下载:http://code.662p.com/list/11_1.html [/td][td] [/td][td] [/td][td] 详细说明:http ...
- NSMutableDictionary 排序问题
NSMutableDictionary 默认情况下是按字母的顺序进行排序的 (a-z)的默认排序如何自定义排序呢? 第一种,利用数组的sortedArrayUsingComparator调用 NSCo ...
- 数据库_11_1~10总结回顾+奇怪的NULL
校对集问题: 比较规则:_bin,_cs,_ci利用排序(order by) 另外两种登录方式: 奇怪的NULL: NULL的特殊性: