CodeForces - 446A DZY Loves Sequences(dp)
2024-10-08 17:54:59
题意:给定一个序列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;
}
最新文章
- 达梦7的试用 与SQLSERVER的简单技术对比
- scikit-learn决策树算法类库使用小结
- ofbiz进击 第六节。 --OFBiz配置之[widget.properties] 配置属性的分析
- debugging tools
- curl模拟Http请求
- ASP.NET Redis 开发(转载)
- LinkedHashSet的实现原理
- EF错误
- js 自定义html标签属性
- MyDAL - 快速使用
- Vue 封装可向左向右查看图片列表的组件
- shell速查
- mariadb(第五章)视图、事物、索引、外键
- 笔记本1050ti显卡安装最新驱动版本 (415.27) 记录 NVIDIA显卡GeForce系列 (Ubuntu18.04)
- Unity3D笔记 Collect
- 网络虚拟化中的 offload 技术:LSO/LRO、GSO/GRO、TSO/UFO、RSS、VXLAN
- week6:Diagnosing Bias vs. Variance难点记录
- 微信小程序 功能函数 替换字符串内的指定字符
- Swift之沙盒与数据存储
- Java之深入JVM(6) - 字节码执行引擎(转)