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 ...
随机推荐
- uniq 去除重复行
1.命令功能 uniq可以输出或忽略文件中的重复行,经常需要使用sort先对文件进行排序,然后使用uniq去重并计数. 2.语法格式 uniq option input uniq 选项 ...
- 锁、volatile、CAS的比较
一.锁 锁是一种悲观的机制.为多线程提供了互斥的访问机制.多个线程同时竞争锁时,没获得锁的线程将会被挂起(智能的JVM会根据之前获取锁操作中对锁的持有时间长短来判断是使线程挂起还是自旋) 锁的劣势:1 ...
- 三星GT S7562 PIN 解锁方法
三星GT S7562 PIN 解锁方法 请认真阅读完下文再进行操作,操作基本安全,请保证你手机电池有电续航超过1小时 首先把内存开和电话卡取出(以防万一数据丢失) 关机状态下: 同时按音量上下键 加 ...
- Python---基础-运算符int和range函数
这行代码是什么意思 if not (money<100) money >= 100 -------------------------------假设有x = 1, y = 2, z = ...
- mysql服务设置远程连接
一.前期准备 1.虚拟机/物理机 mysql环境(非本机)2.本机 navicat软件(验证远程连接) 二 .mysql配置 1.在远程主机的本机 使用root用户连接mysql mysql ...
- nyoj 119: 士兵杀敌(三) 【RMQ模板】
题目链接 贴个板子.. #include<bits/stdc++.h> using namespace std; int n,q; ],d1[][],d2[][]; void RMQ_in ...
- 5G即将到来,你还会购买4G手机吗?
科技在不断进步,通信技术也是如此,5G网络将于明年下半年开始测试部署,4G手机是否值得更换呢?三星上周发布了Galaxy Note 9智能手机,这也给消费者带来了一个难题:到底是现在花上1000美元将 ...
- MySQL跑得慢的原因分析
第一点,硬件太老 硬件我们这里主要从CPU.内存.磁盘三个方面来说下,还有一些因素比如网卡,机房网络等因为文章篇幅关系,就不一一介绍了,以后还有机会可以聊. 首先我们来看下MySQL对CPU的利用特点 ...
- Anaconda概念和使用方法
Anaconda概述 Anaconda是一个用于科学计算的Python发行版,支持 Linux, Mac, Windows系统,提供了包管理与环境管理的功能,可以很方便地解决多版本python并存.切 ...
- 【HDOJ6664】Andy and Maze(color coding)
题意:给定一张n点m边的无向带权图,问从任意结点出发,不能走已经经过的点,共经过k个点的最长路径的值 n,m<=1e4,k<=6 思路:color coding算法 考虑每次给每个点随机编 ...