题意:给定一个序列a,求最长的连续子序列b的长度,在至多修改b内一个数字(可修改为任何数字)的条件下,使得b严格递增。

分析:

1、因为至多修改一个数字,假设修改a[i],

2、若能使a[i] < a[i + 1] 且 a[i] > a[i - 1],则修改a[i]能得到的最长连续子序列长度为l[i - 1] + r[i + 1] + 1。

3、若不满足条件2,则修改a[i]能得到的最长连续子序列长度应取l[i - 1] + 1(即从a[i-1]能向左延伸的最大长度加上a[i]形成的序列)和r[i + 1] + 1的最大值。

4、枚举a[i]取最大值即可。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1e5 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN];
int l[MAXN];//从a[i]向左延伸的长度
int r[MAXN];//从a[i]向右延伸的长度
int main(){
int n;
scanf("%d", &n);
l[0] = 1;
for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
if(i){
if(a[i] > a[i - 1]){
l[i] = l[i - 1] + 1;
}
else{
l[i] = 1;
}
}
}
if(n == 1){
printf("1\n");
return 0;
}
r[n - 1] = 1;
for(int i = n - 2; i >= 0; --i){
if(a[i] < a[i + 1]){
r[i] = r[i + 1] + 1;
}
else{
r[i] = 1;
}
}
int ans = Max(r[1], l[n - 2]) + 1;//修改a[0]或a[n-1]能得到的最长连续序列长度的最大值
for(int i = 1; i < n - 1; ++i){
if(a[i + 1] - a[i - 1] > 1){
ans = Max(ans, l[i - 1] + r[i + 1] + 1);
}
else{
ans = Max(ans, l[i - 1] + 1);
ans = Max(ans, r[i + 1] + 1);
}
}
printf("%d\n", ans);
return 0;
}

  

CodeForces - 446A DZY Loves Sequences(dp)的更多相关文章

  1. codeforces#FF DIV2C题DZY Loves Sequences(DP)

    题目地址:http://codeforces.com/contest/447/problem/C C. DZY Loves Sequences time limit per test 1 second ...

  2. Codeforces 446A. DZY Loves Sequences (线性DP)

    <题目链接> 题目大意: 给定一个长度为$n$的序列,现在最多能够改变其中的一个数字,使其变成任意值.问你这个序列的最长严格上升子段的长度是多少. #include <bits/st ...

  3. codeforces 446A DZY Loves Sequences

    vjudge 上题目链接:codeforces 446A 大意是说最多可以修改数列中的一个数,求最长严格递增的连续子序列长度. 其实就是个 dp 的思想,想好思路后交上去没想到一直 wa 在第二个测试 ...

  4. CodeForces 446A DZY Loves Sequences (DP+暴力)

    题意:给定一个序列,让你找出一个最长的序列,使得最多改其中的一个数,使其变成严格上升序列. 析:f[i] 表示以 i 结尾的最长上升长度,g[i] 表示以 i 为开始的最长上升长度,这两个很容易就求得 ...

  5. Codeforces Round #FF 446A DZY Loves Sequences

    预处理出每一个数字能够向后延伸多少,然后尝试将两段拼起来. C. DZY Loves Sequences time limit per test 1 second memory limit per t ...

  6. Codeforces 447C - DZY Loves Sequences

    447C - DZY Loves Sequences 思路:dp 代码: #include<bits/stdc++.h> using namespace std; #define ll l ...

  7. CodeForces 444C. DZY Loves Physics(枚举+水题)

    转载请注明出处:http://blog.csdn.net/u012860063/article/details/37509207 题目链接:http://codeforces.com/contest/ ...

  8. CodeForces 445B. DZY Loves Chemistry(并查集)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://codeforces.com/problemset/prob ...

  9. CodeForces - 710E Generate a String (dp)

    题意:构造一个由a组成的串,如果插入或删除一个a,花费时间x,如果使当前串长度加倍,花费时间y,问要构造一个长度为n的串,最少花费多长时间. 分析:dp[i]---构造长度为i的串需要花费的最短时间. ...

随机推荐

  1. pta 拯救007(Floyd)

    7-9 拯救007(25 分) 在老电影“007之生死关头”(Live and Let Die)中有一个情节,007被毒贩抓到一个鳄鱼池中心的小岛上,他用了一种极为大胆的方法逃脱 —— 直接踩着池子里 ...

  2. Java8 使用LocalDate计算两个日期间隔多少年,多少月,多少天

    最近项目遇到一个需要计算两个日期间隔的期限,需要计算出,整年整月整日这样符合日常习惯的说法,利用之前的Date和Calendar类会有点复杂,刚好项目使用了JDK8,那就利用起来这个新特性,上代码: ...

  3. 058、Java中定义一个没有参数没有返回值的方法

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  4. 2 JVM 运行机制

  5. 基础知识 SafeSEH DEP ASLR SEHOP

    大多是0day书上抄的 1.SafeSEH 机制: 首先:内存中有SEH表的备份(加密过的) 在调用异常出来函数前,RtlDispatchException()函数中的行为: Ⅰ.检查异常处理链是否位 ...

  6. HihoCoder#1052:基因工程

    HihoCoder#1052:基因工程 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho正在进行一项基因工程实验.他们要修改一段长度为N的DNA序列,使得这段 ...

  7. tools.eclipse.内存配置

    环境:jdk1.7+eclipse luna 选择:Run ->Run Configurations, 在弹出框右侧中选择Arguments, 在VM arguments最后加入 -Xms256 ...

  8. 06--Java--Scanner类读入控制台

    Scanner类读入控制台 1.什么是Scanner类 Scanner类是java中从控制台读入用户输入的类 import java.util.Scanner; public class a_Lear ...

  9. 七十九、SAP中数据库操作之更新数据,UPDATE的用法

    一.我们查看SFLIGHT数据库,比如我们需要改这条数据 二.代码如下 三.执行效果如下,显示“数据更新成功” 四.我们来看一下SFLIGHT数据库,发现已经由DEM更改为了AAA了

  10. Flutter如何引用第三方库并使用

    Flutter如何引用第三方库并使用 https://www.jianshu.com/p/bbda7794345e Flutter官网点击访问Flutter教程(一)Flutter概览Flutter教 ...