题目链接:http://codeforces.com/contest/1272/problem/D

题目大意:

给你一个长度为 \(n\) 的数组,你最多删除一个元素(也可以不删),求此条件限制下的最长上升子串长度。

解题思路:

本题涉及算法:动态规划。

首先这里有一个条件“你最多可以删除一个元素”,这个条件会造成我们很多的困扰,所以为了避免困扰,我们先尝试在没有这个条件的情况下解决问题。

在我们没有删除元素的权限下,数组 \(a\) 中的 \(n\) 个元素是固定的,所以此时我们可以定义状态 \(f[i]\) 表示以 \(a[i]\) 结尾并且包含 \(a[i]\) 的最长上升子串的长度,那么我们可以发现(设数组坐标从 \(1\) 开始):

\(f[1]=1\);

当 \(i \gt 1\) 时,

  • 如果 \(a[i-1] < a[i]\) ,则 \(f[i] = f[i-1]+1\)
  • 否则,\(f[i]=1\)

代码实现:

f[1] = 1;
for (int i = 2; i <= n; i ++) {
if (a[i-1] < a[i]) f[i] = f[i-1]+1;
else f[i] = 1;
}

然后我们需要求的最长上升子串长度就是所有 \(f[i]\) 中最大的那个。

稍等一下,我们暂时还是不加上“你最多可以删除一个元素”这个条件。

在加上这个条件之前,我们再定义一个状态 \(g[i]\) 表示以 \(a[i]\) 开头并且包含 \(a[i]\) 的最长上升子串的长度,那么,我们可以得到状态转移方程:

\(g[n] = 1\);

当 \(i < n\) 时,

  • 如果 \(a[i] < a[i+1]\),则 \(g[i] = g[i+1]+1\);
  • 否则,\(g[i] = 1\)

代码实现(注意 \(g[i]\) 需要从 \(n\) 到 \(1\) 反着推):

g[n] = 1;
for (int i = n-1; i >= 1; i --) {
if (a[i] < a[i+1]) g[i] = g[i+1]+1;
else g[i] = 1;
}

那么我们求完 \(f[i]\) 和 \(g[i]\) 之后呢,我们再来加回“你最多可以删除一个元素”这个条件。

首先,如果我们不删除元素,那么答案就是所有 \(f[i]\) 中的最大值,我们开一个变量 \(ans = \max(f[i])\)。

其次,如果我们删除元素的坐标是 \(i\) ,我们假设删除元素后的最长上升子串对应为 \(a[l]\) 到 \(a[r]\),

那么如果 \(i\) 不满足 \(l < i < r\) 的条件,那么我删或者不删 \(a[i]\) 对我答案丝毫不影响(仍然是 \(ans = \max(f[i])\))。

那么什么时候会对答案有影响呢?

就是当 \(1 < i < n\) 且 \(a[i-1] < a[i+1]\) 的时候,我删除 \(a[i]\) 能够使得 \(f[i-1] + g[i+1] > ans\) 的时候,说明删除元素 \(a[i]\) 达到了增长最长上升子串的效果,此时我们更新 \(ans = f[i-1] + g[i+1]\) 。

综上所述,答案应该是

  • \(\max(f[i])\) (其中 \((1 \le i \le n)\))

  • \(\max(f[i-1]+g[i+1])\) (其中 \(1 \lt i \lt n\) 且 \(a[i-1] < a[i+1]\))

的较大值。

实现代码如下:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200020;
int n, a[maxn], f[maxn], g[maxn], ans;
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
f[1] = 1;
for (int i = 2; i <= n; i ++) {
if (a[i-1] < a[i]) f[i] = f[i-1]+1;
else f[i] = 1;
}
g[n] = 1;
for (int i = n-1; i >= 1; i --) {
if (a[i] < a[i+1]) g[i] = g[i+1]+1;
else g[i] = 1;
}
for (int i = 1; i <= n; i ++) ans = max(ans, f[i]);
for (int i = 2; i < n; i ++) if (a[i-1] < a[i+1]) ans = max(ans, f[i-1] + g[i+1]);
cout << ans << endl;
return 0;
}

CF1272D. Remove One Element 题解 动态规划的更多相关文章

  1. [CodeForces - 1272D] Remove One Element 【线性dp】

    [CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...

  2. [LeetCode] Remove Element题解

    Remove Element: Given an array and a value, remove all instances of that value in place and return t ...

  3. Codeforces Round #605 (Div. 3) D. Remove One Element(DP)

    链接: https://codeforces.com/contest/1272/problem/D 题意: You are given an array a consisting of n integ ...

  4. 洛谷P1002 过河卒 题解 动态规划

    题目链接:https://www.luogu.com.cn/problem/P1002 题目大意 棋盘上\(A\)点有一个过河卒,需要走到目标\(B\)点.卒行走的规则:可以向下.或者向右.同时在棋盘 ...

  5. [LeetCode]Longest Palindromic Substring题解(动态规划)

    Longest Palindromic Substring: Given a string s, find the longest palindromic substring in s. You ma ...

  6. how to remove an element in lxml

    import lxml.etree as et xml=""" <groceries> <fruit state="rotten"& ...

  7. codeforces2B.The least round way 题解 动态规划/模拟

    题目出处:http://codeforces.com/problemset/problem/2/B 题目描述 给你一个 \(n \times n\) 的二维数组,它包含的元素都是非负整数.你需要寻找一 ...

  8. 洛谷P1280 尼克的任务 题解 动态规划/最短路

    作者:zifeiy 标签:动态规划.最短路 题目链接:https://www.luogu.org/problem/P1280 题目大意: 有k个任务分布在第1至n这n个时间点,第i个任务的于第 \(P ...

  9. 洛谷P5664 Emiya 家今天的饭 题解 动态规划

    首先来看一道题题: 安娜写宋词 题目背景 洛谷P5664 Emiya 家今天的饭[民间数据] 的简化版本. 题目描述 安娜准备去参加宋词大赛,她一共掌握 \(n\) 个 词牌名 ,并且她的宋词总共有 ...

随机推荐

  1. android学习——android 常见的错误 和 解决方法

    1. Application does not specify an API level requirement! 解决方法:AndroidManifest.xml中 加入: <uses-sdk ...

  2. hdu 4114 Disney's FastPass(最短路+状态压缩)

    Disney's FastPass Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  3. centos下iptables安装

    [root@localhost ~]# yum install iptables -y[root@localhost ~]# yum install iptables-services 查看安装情况 ...

  4. Keras框架下的保存模型和加载模型

    在Keras框架下训练深度学习模型时,一般思路是在训练环境下训练出模型,然后拿训练好的模型(即保存模型相应信息的文件)到生产环境下去部署.在训练过程中我们可能会遇到以下情况: 需要运行很长时间的程序在 ...

  5. servicemix 实例 -- 参考open source ESBs in action这本书

    1. 项目结构 2. bean服务处理单元 1)Person类 package esb.chapter3; import java.io.StringWriter; import javax.xml. ...

  6. H3C NAT Server

  7. ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.

    ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.You a ...

  8. python3快捷键

    原文:https://blog.csdn.net/pipisorry/article/details/39909057 PyCharm3.0默认快捷键(翻译的) PyCharm Default Key ...

  9. Codeforces Round #178 (Div. 2)

    A. Shaass and Oskols 模拟. B. Shaass and Bookshelf 二分厚度. 对于厚度相同的书本,宽度竖着放显然更优. 宽度只有两种,所以枚举其中一种的个数,另一种的个 ...

  10. H3C 单区域OSPF配置示例二