Codeforces Round #FF (Div. 1) A. DZY Loves Sequences 动态规划
A. DZY Loves Sequences
题目连接:
http://www.codeforces.com/contest/446/problem/A
Description
DZY has a sequence a, consisting of n integers.
We'll call a sequence ai, ai + 1, ..., aj (1 ≤ i ≤ j ≤ n) a subsegment of the sequence a. The value (j - i + 1) denotes the length of the subsegment.
Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.
You only need to output the length of the subsegment you find.
Input
The first line contains integer n (1 ≤ n ≤ 105). The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
In a single line print the answer to the problem — the maximum length of the required subsegment.
Sample Input
6
7 2 3 1 5 6
Sample Output
5
Hint
题意
让你找到一个区间,你可以改变这个区间的一个数,然后使得这个区间是严格上升的
且这个区间一定是最长的,输出区间长度
题解:
dp1[i]表示从左边开始的最长上升子串,dp2[i]是右边开始的最长上升子串
然后我们枚举i,那么答案就显然是在a[i-1]<a[i+1]-1的情况下,ans=max(ans,dp1[i-1]+dp2[i+1]+1)这个玩意儿
然后不停转移就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int dp1[maxn],dp2[maxn],a[maxn];
int main()
{
int n;
scanf("%d",&n);a[0]=1e9+7,a[n+1]=1e9+7;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
if(n==1)return puts("1"),0;
if(n==2)return puts("2"),0;
int ans=0;
for(int i=1;i<=n;i++)
{
dp1[i]=1;
if(a[i]>a[i-1])dp1[i]=dp1[i-1]+1;
ans=max(ans,dp1[i]);
}
for(int i=n;i>=1;i--)
{
dp2[i]=1;
if(a[i]<a[i+1])dp2[i]=dp2[i+1]+1;
ans=max(ans,dp2[i]);
}
for(int i=1;i<=n;i++)
{
if(a[i-1]<a[i+1]-1)
ans=max(ans,dp1[i-1]+dp2[i+1]+1);
}
for(int i=2;i<=n;i++)ans=max(ans,dp2[i]+1);
for(int i=1;i<n;i++)ans=max(ans,dp1[i]+1);
cout<<ans<<endl;
}
Codeforces Round #FF (Div. 1) A. DZY Loves Sequences 动态规划的更多相关文章
- DP Codeforces Round #FF (Div. 1) A. DZY Loves Sequences
题目传送门 /* DP:先用l,r数组记录前缀后缀上升长度,最大值会在三种情况中产生: 1. a[i-1] + 1 < a[i+1],可以改a[i],那么值为l[i-1] + r[i+1] + ...
- Codeforces Round #FF (Div. 1) A. DZY Loves Sequences
题目链接: http://www.codeforces.com/contest/446/problem/A 题解: dp1[x]表示以x结尾的最大严格升序连续串,dp2[x]表示以x开头的最大严格升序 ...
- Codeforces Round #FF (Div. 2) C. DZY Loves Sequences
解题报告:输入一个数列,选取一个子数列,要求最多只能改动这个子数列中的一个数,使得这个子数列是严格的升序的(严格升序没有相等的) 我的做法是,第一步把这个 数列的每个升序的子数列都找出来,然后看这些子 ...
- Codeforces Round #FF (Div. 1) B. DZY Loves Modification 优先队列
B. DZY Loves Modification 题目连接: http://www.codeforces.com/contest/446/problem/B Description As we kn ...
- Codeforces Round #FF (Div. 2) D. DZY Loves Modification 优先队列
D. DZY Loves Modification time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Codeforces Round #FF (Div. 1) B. DZY Loves Modification
枚举行取了多少次,如行取了i次,列就取了k-i次,假设行列单独贪心考虑然后相加,那么有i*(k-i)个交点是多出来的:dpr[i]+dpc[k-i]-i*(k-i)*p 枚举i取最大值.... B. ...
- Codeforces Round #FF (Div. 2):B. DZY Loves Strings
B. DZY Loves Strings time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #FF (Div. 2)__E. DZY Loves Fibonacci Numbers (CF447) 线段树
http://codeforces.com/contest/447/problem/E 题意: 给定一个数组, m次操作, 1 l r 表示区间修改, 每次 a[i] + Fibonacci[i-l ...
- Codeforces Round #FF (Div. 2) A. DZY Loves Hash
DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the ...
随机推荐
- 【原创】Linux环境下的图形系统和AMD R600显卡编程(1)——Linux环境下的图形系统简介
Linux/Unix环境下最早的图形系统是Xorg图形系统,Xorg图形系统通过扩展的方式以适应显卡和桌面图形发展的需要,然而随着软硬件的发展,特别是嵌入式系统的发展,Xorg显得庞大而落后.开源社区 ...
- 03 Editor plugins and IDEs 编辑器插件和 ide
Editor plugins and IDEs 编辑器插件和 ide Introduction 介绍 Options 选项 Introduction 介绍 This document list ...
- 经典面试题:js继承方式下
上一篇讲解了构造函数的继承方式,今天来讲非构造函数的继承模式. 一.object()方法 json格式的发明人Douglas Crockford,提出了一个object()函数,可以做到这一点. fu ...
- Java编程思想第四版第二章练习题答案
练习1:创建一个类,它包含一个int域和一个char域,它们都没有被初始化.将他们的值打印出来,以验证Java执行了默认初始化 public class JavaThinking { private ...
- 数据迁移之Sqoop
一 简介 Apache Sqoop(TM)是一种用于在Apache Hadoop和结构化数据存储(如关系数据库)之间高效传输批量数据的工具 . 官方下载地址:http://www.apache.org ...
- 【BZOJ】1152: [CTSC2006]歌唱王国Singleland
题解 读错题了,是最后留下一个牛人首长歌颂他,和其他人没有关系,t就相当于数据组数 结论题,具体可看 https://www.zhihu.com/question/59895916/answer/19 ...
- 004 Ajax中传输格式为JSON
一: 1.介绍 2.嵌套 3.json解析 4.优缺点 二:json功能程序测试 1.设计 2.程序 <!DOCTYPE html> <html> <head> & ...
- [python selenium] 操作方法整理
个人笔记,摘抄自虫师python selenum,仅供个人参考 1.安装: pip install selenium 下载webdriver # webdriver 下载并放置在python主目录 · ...
- ubuntu下安装低级版本gcc/g++ 并随意切换
来自:http://blog.sina.com.cn/s/blog_6cee149d010129bl.html 发现Android的版本中编译Host的程序经常因为本机的Gcc版本过高,需要这样那样的 ...
- PHP 数组的添加和读取
在实际的开发中,会经常使用数组的添加和读取.这里把经常使用的操作记下来,以备以后查阅. <?php //一维数值数组 $list = array('wang','god'); $list[] = ...