CF1272D. Remove One Element 题解 动态规划
题目链接: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 题解 动态规划的更多相关文章
- [CodeForces - 1272D] Remove One Element 【线性dp】
[CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...
- [LeetCode] Remove Element题解
Remove Element: Given an array and a value, remove all instances of that value in place and return t ...
- 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 ...
- 洛谷P1002 过河卒 题解 动态规划
题目链接:https://www.luogu.com.cn/problem/P1002 题目大意 棋盘上\(A\)点有一个过河卒,需要走到目标\(B\)点.卒行走的规则:可以向下.或者向右.同时在棋盘 ...
- [LeetCode]Longest Palindromic Substring题解(动态规划)
Longest Palindromic Substring: Given a string s, find the longest palindromic substring in s. You ma ...
- how to remove an element in lxml
import lxml.etree as et xml=""" <groceries> <fruit state="rotten"& ...
- codeforces2B.The least round way 题解 动态规划/模拟
题目出处:http://codeforces.com/problemset/problem/2/B 题目描述 给你一个 \(n \times n\) 的二维数组,它包含的元素都是非负整数.你需要寻找一 ...
- 洛谷P1280 尼克的任务 题解 动态规划/最短路
作者:zifeiy 标签:动态规划.最短路 题目链接:https://www.luogu.org/problem/P1280 题目大意: 有k个任务分布在第1至n这n个时间点,第i个任务的于第 \(P ...
- 洛谷P5664 Emiya 家今天的饭 题解 动态规划
首先来看一道题题: 安娜写宋词 题目背景 洛谷P5664 Emiya 家今天的饭[民间数据] 的简化版本. 题目描述 安娜准备去参加宋词大赛,她一共掌握 \(n\) 个 词牌名 ,并且她的宋词总共有 ...
随机推荐
- uva 10566 Crossed Ladders (二分)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- oracle SELECT子句中避免使用 ‘ * ‘
当你想在SELECT子句中列出所有的COLUMN时,使用动态SQL列引用 ‘*’ 是一个方便的方法. 不幸的是,这是一个非常低效的方法. 实际上,ORACLE在解析的过程中, 会将’*’ 依次转换成所 ...
- 2018-6-24-WPF-使用RPC调用其他进程
title author date CreateTime categories WPF 使用RPC调用其他进程 lindexi 2018-06-24 14:41:29 +0800 2018-2-13 ...
- [转]Android Studio实现代码混淆
1,在build.grandle添加,其中规则写在proguard-rules.pro中,也可以自定义一个文件,将其代替,比如eclipse常用的 proguard-project.txt: bui ...
- Python--day67--内容回顾
- Python--day64--内容回顾
1,内容回顾 1.ORM外键操作 图书表和出版社表 多对一的关系 #书 class Book(models.Model): id = models.AutoField(primary_key=True ...
- python基础七之copy
浅拷贝 没有嵌套,则copy后完全不同,有嵌套,则copy后本体不同,嵌套相同. l1 = [1, 2, [4, 5, 6], 3] l2 = l1.copy() print(l1 is l2) # ...
- P1100 三连击
题目描述 我们假设一个三位整数 \(N(100 \le N \le 999)\) ,它的百位上的数字是 \(A\) ,十位上的数字是 \(B\) ,个位上的数字是 \(C\) ,如果 \(A\) , ...
- java 声明多个泛型类型和通配符
若一个类中多个字段需要不同的泛型声明,则在声明类的时候指定多个泛型类型即可: 格式: public interface IDAO<PK, T> { PK add(T t); void re ...
- P1038 间谍入侵
题目描述 爱丽丝魔法王国成立10周年,于是决定矩形国庆大阅兵. 在国庆大阅兵期间,为了防止暗黑王国的间谍乔装成平民混入,需要对每一个进城的人做检测. 因为暗黑王国的人长得和爱丽丝魔法王国的人长得很像, ...