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.
思路:预处理出包含第i个元素的左边最长连续递增长度 和 包含第i个元素的右边最长连续递增长度, 枚举可以任意改变的位置 pos, 然后判断该位置的左右两边是否可以连接以满足递增
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
const int N = 100005;
int a[N], lt[N], rt[N], n;
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
lt[1] = 1;
for(int i = 2; i <= n; ++i) { //预处理i的左边且包含i的连续最长递增数列的长度
if(a[i] > a[i - 1]) lt[i] = lt[i - 1] + 1;
else lt[i] = 1;
} // for(int i = 1; i <= n; ++i) cout << lt[i] << ' ' ; cout << endl;
rt[n] = 1;
for(int i = n - 1; i >= 1; --i) { ////预处理i的右边且包含i的连续最长递增数列的长度
if(a[i] < a[i + 1]) rt[i] = rt[i + 1] + 1;
else rt[i] = 1;
}
// for(int i = 1; i <= n; ++i) cout << rt[i] << ' ' ; cout << endl;
int ans = max(rt[2] + 1, lt[n - 1] + 1);
for(int i = 2; i < n; ++i) {//枚举可任意更换的位置
if(a[i - 1] + 1 >= a[i + 1]) ans = max(ans, max(lt[i - 1], rt[i + 1]) + 1); // 若i的两边不能衔接, 取较大的长度
else ans = max(ans, lt[i - 1] + rt[i + 1] + 1);//否则为答案为 两段和
}
printf("%d\n", ans);
}
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] + ...
- C. DZY Loves Sequences
C. DZY Loves Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
随机推荐
- 【python】解压文件
参考:http://essen.iteye.com/blog/1941489 tarfile模块 具体使用方法: https://docs.python.org/2/library/tarfile.h ...
- 甲鱼od19篇随笔
在一个程序里会有多个对话框,这时要准确的判断要找的对话框就比较困难了所以这里就需要借助 1:Resource Hacker工具来准确的定位涉及到的对话框 2:在od中查找指令,然后在所有找到的指令上下 ...
- 9.27js拓展、bootstrap菜鸟教程
js(点击挂上 与 点击移除) <div id="dd" style="width:200px; height:200px; background-color:#6 ...
- C 替换字符方法
// 444.cpp : Defines the entry point for the console application. // #include "stdafx.h" # ...
- iOS 简单提示view
+(void)showMessage:(NSString *)message{ UIWindow * window = [UIApplication sharedApplication].key ...
- NYOJ题目1080年龄排序
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtMAAAJVCAIAAACTf+6jAAAgAElEQVR4nO3dO1Lj3NbG8W8Szj0QYg ...
- python简介
python起源 作者Guido van Rossum,荷兰人 在创建python之初,1989年12月份,Guido只是想用编程来打发圣诞的闲暇时光.Guido也希望能有一门语言既能够像C语言那样, ...
- Linux安装xwindow图形界面
在我们安装Linux系统时,刚开始的时候可能没有安装图形界面的需要,但在使用过程中却有可能产生这种需求.那么这种情况下,我们需不需要重新安装Linux系统来安装桌面呢?答案是不需要.下面我将交大家在已 ...
- 用java来删除数组中指定的元素
public static void main(String[] args){ String[] a = new String[]{"1","5" ...
- MVC – 9.mvc整体请求流程
1.请求管道 2~5微软自己的验证,我们一般不用. 在全局配置文件中-已经配置一个路由过滤器-为第7个事件注册了路由方法 1.在application_start中向静态路由表注册了路由数据,在管 ...