Codeforces Round #FF 446A 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.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; int n,a[110000],dp[110000]; int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",a+i);
int nx=-1;
for(int i=0;i<n;i++)
{
if(nx>i)
{
dp[i]=nx-i;
continue;
}
int j=i;
while(j+1<n&&a[j]<a[j+1]) j++;
dp[i]=j-i+1;
nx=max(nx,j+1);
}
int ans=dp[0];
for(int i=0;i<n;i++)
{
int p=dp[i]+i-1;
if(p-1>=0&&p+1<n&&a[p+1]>a[p-1]+1)
{
ans=max(ans,dp[i]-1+dp[p+1]+1);
}
if(p+2<n&&a[p+2]>a[p]+1)
{
ans=max(ans,dp[i]+dp[p+2]+1);
}
if(p+1<n||i-1>=0) ans=max(ans,dp[i]+1);
i=p;
}
printf("%d\n",ans);
return 0;
}
Codeforces Round #FF 446A DZY Loves Sequences的更多相关文章
- codeforces 446A DZY Loves Sequences
vjudge 上题目链接:codeforces 446A 大意是说最多可以修改数列中的一个数,求最长严格递增的连续子序列长度. 其实就是个 dp 的思想,想好思路后交上去没想到一直 wa 在第二个测试 ...
- Codeforces 446A. DZY Loves Sequences (线性DP)
<题目链接> 题目大意: 给定一个长度为$n$的序列,现在最多能够改变其中的一个数字,使其变成任意值.问你这个序列的最长严格上升子段的长度是多少. #include <bits/st ...
- CodeForces 446A DZY Loves Sequences (DP+暴力)
题意:给定一个序列,让你找出一个最长的序列,使得最多改其中的一个数,使其变成严格上升序列. 析:f[i] 表示以 i 结尾的最长上升长度,g[i] 表示以 i 为开始的最长上升长度,这两个很容易就求得 ...
- CodeForces - 446A DZY Loves Sequences(dp)
题意:给定一个序列a,求最长的连续子序列b的长度,在至多修改b内一个数字(可修改为任何数字)的条件下,使得b严格递增. 分析: 1.因为至多修改一个数字,假设修改a[i], 2.若能使a[i] < ...
- 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 动态规划
A. DZY Loves Sequences 题目连接: http://www.codeforces.com/contest/446/problem/A Description DZY has a s ...
- 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#FF DIV2C题DZY Loves Sequences(DP)
题目地址:http://codeforces.com/contest/447/problem/C C. DZY Loves Sequences time limit per test 1 second ...
- [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 ...
随机推荐
- CGI原理解析系列之中的一个----CGI怎样获取WEBserver数据
//gcc get_post.c -o get_post.ums; #include <stdio.h> #include <stdlib.h> #include <un ...
- flex eclipse综合spring入门
首先下载FlashBuilder_4_7_LS10_win64.exe试了几eclipse安装没有成功插头,含有myeclipse8.5.spring sts2.9.2.eclipse3.5.j2ee ...
- 左右 Java 于 finally 深度分析语句块
首先,让我们来问你一个问题:finally 声明块将运行? 很多人认为 finally 语句块是一定要运行.其中还包括了一些非常有经验的 Java 程序猿.不幸的是,没有像很多像人们想象,对于这个问题 ...
- swift 笔记 (十九) ——
协议
协议(Protocols) 协议仅是用定义某些任务或者是功能必须的方法和属性. 类似于java里的interface的作用.但协议并不会实现详细的功能. 我猜这个名字源于OO中提到的"契约& ...
- Android 实现蘑菇街购物车动画效果
版本号:1.0 日期:2014.8.6 版权:© 2014 kince 转载注明出处 使用过蘑菇街的用户基本上都知道有一个增加购物车的动画效果,此处不详细描写叙述想知道的能够去下载体验一下. 1 ...
- Android日志收集功能设计和实施报告(总)
几乎所有的应用程序开发者都知道"用户体验"重视,为了增强用户体验是从全面的监测和报告制度密不可分,在这些日志(控制Crash报告)是主要的问题跟踪和解决工具. 日志上报系统.首先看 ...
- 全自动Web后门扫描(转)
阅读目录 工具介绍 使用方法 工具介绍 这是一款全自动Web后门查杀工具,基于Python开发 某些较新的后门可能会查杀失败 规则列表来自seay博客 回到顶部 使用方法 1.按恶意代码查杀: pyt ...
- 初识 Cloudera Impala
Impala是Cloudera公司主导开发的新型查询系统,它提供SQL语义,能查询存储在Hadoop的HDFS和HBase中的PB级大数据.已有的Hive系统尽管也提供了SQL语义,但因为Hive底层 ...
- 什么是WEBserver? 经常使用的WEBserver有哪些?
什么是WEBserver? 经常使用的WEBserver有哪些? 一.什么是WEBserver Webserver能够解析HTTP协议.当Webserver接收到一个HTTP请求,会返回一个HTTP响 ...
- Hadoop之——HBase注意事项
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46447573 1.HBase(NoSQL)的数据模型 1.1 表(table) 存 ...