题目链接:https://codeforces.com/contest/1437/problem/E

题意

给出一个大小为 \(n\) 的数组 \(a\) 和一个下标数组 \(b\),每次操作可以选择数组 \(b\) 外的任意下标 \(i\) 并将 \(a_i\) 赋值为任意数,问能否经过操作使得数组 \(a\) 严格递增,如果可以,计算所需的最少操作次数。

题解

令 \(c_i = a_i - i\),如果数组 \(a\) 严格递增,那么数组 \(c\) 一定为非递减序。

所以判断下标数组 \(b\) 对应的数组 \(c\) 是否为非递减序,如果是,则有解,选取数组 \(b\) 中两两相邻的数为左右端点,并在数组 \(c\) 内寻找该区间内的最长非下降子序列,其余数则均需要改变。

Tips

数组 \(b\) 可能不包含首尾下标,所以需要在 \(b\) 首尾再添加两个下标使得两两形成的区间可以取到所有数。

代码

#include <bits/stdc++.h>

using namespace std;

int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); int n, k;
cin >> n >> k; vector<int> c(n + 2);
c[0] = -2e9;
for (int i = 1; i <= n; i++) cin >> c[i], c[i] -= i;
c[n + 1] = 2e9; vector<int> b(k + 2);
b[0] = 0;
for (int i = 1; i <= k; i++) cin >> b[i];
b[k + 1] = n + 1; int ans = 0;
for (int i = 0; i < k + 1; i++) {
int l = b[i], r = b[i + 1];
if (c[l] > c[r]) {
cout << -1 << "\n";
return 0;
}
vector<int> lis;
for (int j = l + 1; j < r; j++) {
if (c[l] <= c[j] and c[j] <= c[r]) {
auto it = upper_bound(lis.begin(), lis.end(), c[j]);
if (it == lis.end()) lis.push_back(c[j]);
else *it = c[j];
}
}
ans += (r - l - 1) - lis.size();
}
cout << ans << "\n"; return 0;
}

Educational Codeforces Round 97 (Rated for Div. 2) E. Make It Increasing(最长非下降子序列)的更多相关文章

  1. Educational Codeforces Round 97 (Rated for Div. 2)

    补了一场Edu round. A : Marketing Scheme 水题 #include <cstdio> #include <algorithm> typedef lo ...

  2. Educational Codeforces Round 97 (Rated for Div. 2)【ABCD】

    比赛链接:https://codeforces.com/contest/1437 A. Marketing Scheme 题解 令 \(l = \frac{a}{2}\),那么如果 \(r < ...

  3. Educational Codeforces Round 97 (Rated for Div. 2) D. Minimal Height Tree (贪心)

    题意:有一个从根节点\(BFS\)得来的序列(每次\(bfs\)子节点的时候保证是升序放入队列的),现在让你还原树(没必要和之前相同),问能构造出的最小的树的深度. 题解:不看根节点,我们从第二个位置 ...

  4. Educational Codeforces Round 97 (Rated for Div. 2) C. Chef Monocarp (DP)

    题意:有\(n\)个菜在烤箱中,每个时刻只能将一个菜从烤箱中拿出来,第\(i\)个时刻拿出来的贡献是\(|i-a[i]|\),你可以在任意时刻把菜拿出来,问将所有菜拿出的最小贡献是多少? 题解: 先对 ...

  5. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  6. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  7. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  8. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  9. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

随机推荐

  1. 【递归】P1706全排列问题

    题目相关 题目描述 输出自然数 1 到 n所有不重复的排列,即 n的全排列,要求所产生的任一数字序列中不允许出现重复的数字. 输入格式 一个整数 n**. 输出格式 由 1∼n 组成的所有不重复的数字 ...

  2. 自定义注解,更优雅的使用MP分页功能

    分页功能使用 MP的分页功能是通过MyBatis的插件实现的,使用起来也非常简单.下面先介绍下使用方式. step1:配置分页插件 @Configuration @EnableTransactionM ...

  3. ajax跨域访问http服务--jsonp

    在前面一篇文章<Spring Cloud 前后端分离后引起的跨域访问解决方案>里我们提到使用ajax跨域请求其他应用的http服务,使用的是后台增加注解@CrossOrigin或者增加Co ...

  4. (二)数据源处理1-configparser读取.ini配置文件

    import osimport configparsercurrent_path =os.path.dirname(__file__)#获取config当前文件路径config_file_path = ...

  5. 【SpringBoot】Spring Boot,开发社区讨论交流网站首页。

    初识Spring Boot,开发社区讨论交流网站首页. 文章目录 初识Spring Boot,开发社区讨论交流网站首页. 1.项目简介 2. 搭建开发环境 JDK Apache Maven Intel ...

  6. 【UML】Use Case Diagrams

    文章目录 Use Case Diagrams Introduction Use case Diagram Use Case Diagrams - Actors Use Case Diagrams – ...

  7. top有用的开关控制命令

    [原创]本文为原创博文,转发请注明出处:https://www.cnblogs.com/dingbj/p/top_command.html 今天偶然用到top命令,在动态刷新的界面上输入h顺便看了下帮 ...

  8. 超精讲-逐例分析CS:LAB2-Bomb!(上)

    0. 环境要求 关于环境已经在lab1里配置过了这里要记得安装gdb 安装命令 sudo yum install gdb 实验的下载地址 http://csapp.cs.cmu.edu/3e/labs ...

  9. SGA: allocation forcing component growth分析

    1.问题现象 20年12月31日,数据库应用人员反映2020-12-31 12:40:10存在告警,过了几分钟之后业务恢复正常. 表现的状态:Connect to database time out, ...

  10. 24V转3.3V芯片,同步降压调节器

    PW2312是一个高频,同步,整流,降压,开关模式转换器与内部功率MOSFET.它提供了一个非常紧凑的解决方案,以实现1.5A的峰值输出电流在广泛的输入电源范围内,具有良好的负载和线路调节. PW23 ...