题目链接
题意:
给你一个数组,只能改变数组中一个数,找到最长严格递增子列,输出其区间长度。
思路:
1.dp,对于第i个数,用dp[i][0]保存左边严格递增区间长度(包括i),用dp[i][1]保存右边严格递增区间长度(包括i),若a[i-1] < a[i+1] - 1,则可改变a[i]来合并左右两区间。
2.预处理得到数组中所有严格递增子列并保存其起始位置k、b,当两相邻子列(1)其中任意一个子列长度为1,则可以合并。(2)前一个子列起始位置为k1,b1,后一子列起始位置为k2,b2,若a[b1] < a[k1 + 1] - 1 或 a[b1 - 1] < a[k1 ] - 1,可以合并。当一个子列不能与其他子列合并时,最多只能变为 自身长度 + 1的严格递增子列。
code1:

#include <bits/stdc++.h>
#define fi first
#define se second
#define debug(x) cout << #x << ":" << x << endl;
#define bug cout << "********" << endl;
#define rep(i,a,n) for(int i = a; i <= n; i ++)
#define per(i,a,n) for(int i = n; i >= a; i --)
using namespace std;
typedef long long ll;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const int mod = 1000;
const int maxn = 3e5 + 10;int s[maxn], dp[maxn][2];int main(){ios::sync_with_stdio(false),cin.tie(0);int t, n;cin >> n;for(int i = 1; i <= n; i ++)cin >> s[i];if(n <= 2){cout << n << endl;return 0;}s[n + 1] = inf;int ans = 0;for(int i = 1; i <= n; i ++){if(s[i - 1] < s[i])dp[i][0] = dp[i - 1][0] + 1;else dp[i][0] = 1;}for(int i = n; i >= 1; i --){if(s[i] < s[i + 1])dp[i][1] = dp[i + 1][1] + 1;else dp[i][1] = 1;}for(int i = 1; i <= (n - 1); i ++)ans = max(ans, dp[i][0] + 1);for(int i = 2; i <= n; i ++)ans = max(ans, dp[i][1] + 1);for(int i = 1; i <= n; i ++){if(s[i - 1] < (s[i + 1] - 1))ans = max(ans, dp[i - 1][0] + dp[i + 1][1] + 1);}cout << ans << endl;return 0;
}

code2:

#include <bits/stdc++.h>
#define fi first
#define se second
#define debug(x) cout << #x << ":" << x << endl;
#define bug cout << "********" << endl;
#define rep(i,a,n) for(int i = a; i <= n; i ++)
#define per(i,a,n) for(int i = n; i >= a; i --)
using namespace std;
typedef long long ll;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const int mod = 1000;
const int maxn = 3e5 + 10;int s[maxn];
pair<int,int> p[maxn];int main(){ios::sync_with_stdio(false),cin.tie(0);int t, n;cin >> n;for(int i = 1; i <= n; i ++)cin >> s[i];if(n <= 2){cout << n << endl;return 0;}int j = 1, tot = 0, pos1 ;while(j <= n){pos1 = j;while(s[j] < s[j + 1] && (j + 1) <= n)j ++;p[++ tot] = make_pair(pos1, j);//起始位置j ++;}// for(int i = 1; i <= tot; i ++)cout << p[i].first << " " << p[i].second << endl;int ans = p[1].se - p[1].fi + 1;for(int i = 1; i < tot; i ++){int len1 = p[i].se - p[i].fi + 1, len2 = p[i + 1].se - p[i + 1].fi + 1;if(len1 == 1 || len2 == 1)ans= max(ans, len1 + len2);else{if(s[p[i].second] < (s[p[i + 1].first + 1] - 1) || s[p[i].se - 1] < (s[p[i + 1].fi] - 1))ans = max(ans, len1 + len2);else ans = max(ans, max(len1 + 1, len2 + 1));}}cout << ans << endl;return 0;
}

DZY Loves Sequences (dp)相关推荐

  1. HDU 5230 ZCC loves hacking(DP)

    Problem Description Now, a Codefires round is coming to end. ZCC has got C(0≤C≤106) points by solvin ...

  2. Codeforces Round #162 (Div. 2): D. Good Sequences(DP)

    题意: 给你n个数字,求出最长相邻不互质子序列 思路: 设dp[i]表示以第i个数字结尾的最长子序列长度 考虑不互质的两个数(x,y),它们一定存在相同的质因子 在DP过程中求出R[p]表示p的倍数当 ...

  3. Codeforces 444C DZY Loves colors(分块)

    我们维护一个标记表示区间内的数是否全相同即可. 如果全相同很容易算出 a , b a,b a,b 数组需要更新多少,打标记即可. 否则暴力修改. #include <map> #inclu ...

  4. AtCoder 4169 [ARC100D] Colorful Sequences(dp)

    problem 洛谷链接 solution 逆向考虑.ans=ans=ans= 所有蛋糕中的 aaa 序列出现次数 −-− 在 non−colorfulnon-colorfulnon−colorful ...

  5. Codeforces 447C - DZY Loves Sequences

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

  6. 【CF 149D】Coloring Brackets(dp)

    [CF 149D]Coloring Brackets(dp) D. Coloring Brackets time limit per test 2 seconds memory limit per t ...

  7. ZCC loves cube(cube)

    ZCC loves cube(cube) 题目描述 调戏完了狗,ZCC开始玩起了积木.ZCC的面前有一块n*n的棋盘,他要用这些1*1*1的积木在棋盘上搭出一个宏伟的建筑.积木有三种颜色,ZCC认为一 ...

  8. 求三角形最大面积(DP)

    求三角形最大面积(DP) 在OJ上奇迹般WA了:WA:70. Why? #include <iostream> #include <string.h> using namesp ...

  9. LeetCode 编辑距离 II(DP)

    1. 题目 给你两个单词 s 和 t,请你计算出将 s 转换成 t 所使用的最少操作数. 你可以对一个单词进行如下两种操作: 删除一个字符 替换一个字符 注意: 不允许插入操作 题目保证有解 示例: ...

最新文章

  1. Matlab与线性代数 -- 稀疏矩阵的创建
  2. 用例设计方法及其覆盖率
  3. 不擅长物理科学计算机吗,物理难学否?答案因人而异,高二同学3 + 3选科莫要太随意...
  4. ubuntu四个屏幕设置_Linux_从9个方面来立体式地美化Ubuntu 桌面,总结了一下桌面美化的设置。 - phpStudy...
  5. 在vue-cli中搭建mock服务器
  6. go uintptr unsafe Pointer offset() 的使用
  7. linux脚本打印循环次数,shell脚本编程基础(3)——循环用法
  8. 零配置 之 Spring 概述
  9. java在捕获异常并弹窗_Java捕获异常的问题
  10. C/C++之strcpy功能实现
  11. vue学习日志-过滤器
  12. JAVA调用shell脚本利用ansible修改多节点上的redis参数
  13. 聊聊我是如何编程入门的
  14. 深度学习入门(鱼书)学习笔记
  15. Pycocotools 报error: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Bui
  16. py征途3之填坑(pagerank个人详解)
  17. Ice Lake CPU RESET流程
  18. 文化是一种meme,NFT也是
  19. Tomcat文件服务器上传文件出错
  20. php打印10以内减法表,10以内加减法口诀表(A4纸可以打印)

热门文章

  1. Win系统 - Win10 进入 BIOS 系统(非启动快捷键)
  2. win10热点 ip配置失败
  3. 服务降级,限流,削峰
  4. 程序员掉入传销组织用“代码”求救,同事秒懂
  5. 华硕ac66php服务器,华硕ASUS RT-AC66U无线路由器部署NTP Server最佳实践
  6. V 社秘密开发 Steam 跨系统兼容工具;甲骨文开源 GraphPipe,机器学习模型标准
  7. c+primer 学习笔记 3
  8. macbook proa1708_苹果a1708是哪一年的
  9. 2022-2028年全球与中国智能纺织品产业市场前瞻与投资战略规划分析
  10. 微软服务器系统2018,升级微软2018 Windows10 四月正式版17134(1803)系统的多种方法...