C. DZY Loves Sequences
1 second
256 megabytes
standard input
standard output
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.
The first line contains integer n (1 ≤ n ≤ 105).
The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
In a single line print the answer to the problem — the maximum length of the required subsegment.
6
7 2 3 1 5 6
5
You can choose subsegment a2, a3, a4, a5, a6 and
change its 3rd element (that is a4)
to 4.
本题坑还算比較多的,我用的递推,dp1[i]表示以i结尾的递增序列的长度,dp2[i]表示已i開始的递增序列的长度,要找出最长的仅仅改变一个数就能构成递增序列的序列长度仅仅须要枚举i即可了,代码例如以下
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
typedef long long LL;
using namespace std;
int A[100005];
int dp1[100005],dp2[100005];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(dp1,0,sizeof(dp1));
memset(dp2,0,sizeof(dp2));
scanf("%d",A+1);
dp1[1]=1;
for(int i=2;i<=n;i++)
{
scanf("%d",A+i);
if(A[i]>A[i-1])dp1[i]=dp1[i-1]+1;
else dp1[i]=1;
}
dp2[n]=1;
for(int i=n-1;i>=1;i--)
{
if(A[i]<A[i+1])dp2[i]=dp2[i+1]+1;
else dp2[i]=1;
}
int m=1;
A[0]=0;
A[n+1]=1000000005;
for(int i=1;i<=n;i++){
int t=dp1[i-1]+dp2[i+1]+1;
if(A[i+1]-A[i-1]<2)t=max(dp1[i],dp2[i])+1;
m=max(m,t);
}
cout<<m<<endl;
}
return 0;
}
C. DZY Loves Sequences的更多相关文章
- cf446A DZY Loves Sequences
A. DZY Loves Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
- codeforces#FF DIV2C题DZY Loves Sequences(DP)
题目地址:http://codeforces.com/contest/447/problem/C C. DZY Loves Sequences time limit per test 1 second ...
- Codeforces Round #FF (Div. 2):C. DZY Loves Sequences
C. DZY Loves Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Round #FF 446A DZY Loves Sequences
预处理出每一个数字能够向后延伸多少,然后尝试将两段拼起来. C. DZY Loves Sequences time limit per test 1 second memory limit per t ...
- [CodeForces - 447C] C - DZY Loves Sequences
C - DZY Loves Sequences DZY has a sequence a, consisting of n integers. We'll call a sequence ai, ai ...
- Codeforces 447C - DZY Loves Sequences
447C - DZY Loves Sequences 思路:dp 代码: #include<bits/stdc++.h> using namespace std; #define ll l ...
- 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 s ...
- 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] + ...
- DZY Loves Sequences
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
随机推荐
- 学Python的第五天
最近忙着学MySQL,但是小编也不会放弃学Python!!! 因为热爱所以学习~ 好了各位,进入正题,由于时间问题今天学的不是很多.... #!/usr/bin/env python # -*- co ...
- glob & fnmatch -- 使用Unix style通配符
通配符: ? 匹配单个字符 * 匹配 0+ 个字符 [seq] 匹配属于区间的单个字符 [!seq] 匹配不属于区间的单个字符 注意: "." just a " ...
- 11JSP基础
1.Jsp基础 1.1 简介 Jsp,全称 Java Server Page java服务页面,能提供java服务的页面 jsp vs html html: 由html标签组成的,输出静态内容. js ...
- maven 常用插件 拷贝依赖 拷贝jar包 查看属性 环境变量
1 maven编译后希望将生产的jar包拷贝到指定目录 在pom中配置maven插件 maven-antrun-plugin <build > <plugins> <pl ...
- NOIP原题板刷
update 10.11 我可能已经刷完大部分了,可是这篇blog我也不想更了 这个人很懒,做了很多题但是不想写题解,也不想更blog,所以这篇blog又咕咕了. 把从 \(1997-2017\) 近 ...
- kafka 磁盘写满导致 InternalError
tailf kafka/log/server.log [2019-12-19 17:19:05,121] FATAL (main kafka.server.KafkaServerStartable 1 ...
- springboot整合hibernate案例
1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springBo ...
- Promise和async/await
1.promise对象 promise 对象有三种状态:pending(进行中).fulfilled(已成功)和 rejected(已失败).promise 对象的状态改变,只有两种可能:从 pend ...
- JavaScript中数组的操作方法总汇
Array(数组)是JavaScript中最为常用的类型了.ECMAScript中的数组都是数据的有序列表.数组中可以保存任何类型的数据.数组的大小是可以动态调整的,既可以随着数据的添加自动增长以容纳 ...
- 如何配置报表服务器扩展部署(Reporting Services 配置)
Reporting Services 支持扩展部署模式.该模式允许运行共享单个报表服务器数据库的多个报表服务器实例. 若要创建扩展部署,请使用安装程序和 Reporting Services 配置工具 ...