Educational Codeforces Round 97 (Rated for Div. 2) E. Make It Increasing(最长非下降子序列)
题目链接: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(最长非下降子序列)的更多相关文章
- Educational Codeforces Round 97 (Rated for Div. 2)
补了一场Edu round. A : Marketing Scheme 水题 #include <cstdio> #include <algorithm> typedef lo ...
- Educational Codeforces Round 97 (Rated for Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1437 A. Marketing Scheme 题解 令 \(l = \frac{a}{2}\),那么如果 \(r < ...
- Educational Codeforces Round 97 (Rated for Div. 2) D. Minimal Height Tree (贪心)
题意:有一个从根节点\(BFS\)得来的序列(每次\(bfs\)子节点的时候保证是升序放入队列的),现在让你还原树(没必要和之前相同),问能构造出的最小的树的深度. 题解:不看根节点,我们从第二个位置 ...
- Educational Codeforces Round 97 (Rated for Div. 2) C. Chef Monocarp (DP)
题意:有\(n\)个菜在烤箱中,每个时刻只能将一个菜从烤箱中拿出来,第\(i\)个时刻拿出来的贡献是\(|i-a[i]|\),你可以在任意时刻把菜拿出来,问将所有菜拿出的最小贡献是多少? 题解: 先对 ...
- 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 ...
- 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 ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- 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 ...
随机推荐
- CentOS7上安装jdk,mysql
最近笔者的云服务器由于中毒,重装系统了... 所以就记录下所有服务的搭建过程吧 1.安装jdk 在oracle上下载linux系统的jdk,笔者这里使用的是1.8 https://www.oracle ...
- 常见大中型网络WLAN基本业务实例
组网图形 大中型WLAN网络简介 本文介绍的WLAN网络是指利用频率为2.4GHz或5GHz的射频信号作为传输介质的无线局域网,相对于有线网络的铺设成本高,不便于网络调整和扩展.位置固定,移动性差等缺 ...
- maven 的安装与配置详细步骤
1. 直接搜索maven 2. 进入后点击download界面,这时出现的是当前最新版本, 当然还有以前的版本可供下载 3. 下载解压到你指定的目录后,需要配置一下环境变量. a. 右键此电脑点击属性 ...
- Hash Tables and Hash Functions
Reference: Compuer science Introduction: This computer science video describes the fundamental princ ...
- MongoDB备份(mongodump)与恢复(mongorestore)工具实践
mongodump和mongorestore实践 1.mongodump备份工具 mongodump能够在Mongodb运行时进行备份,它的工作原理是对运行的Mongodb做查询,然后将所有查到的文档 ...
- Mongodb 安装和副本集集群搭建
通用步骤,适用于所有你需要用的软件. 总结为5大步骤: 找到官网-->下载包-->解压-->修改配置-->启动 不懂的,首选官网api,次选百度 1.安装mongodb mon ...
- 【SpringBoot】Spring Boot,开发社区讨论交流网站首页。
初识Spring Boot,开发社区讨论交流网站首页. 文章目录 初识Spring Boot,开发社区讨论交流网站首页. 1.项目简介 2. 搭建开发环境 JDK Apache Maven Intel ...
- P2986 [USACO10MAR]伟大的奶牛聚集(思维,dp)
题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...
- Redis 实战 —— 04. Redis 数据结构常用命令简介
字符串 P39 Redis 的字符串是一个有字节组成的序列,可以存储以下 3 种类型的值:字节串(byte string).整数.浮点数. 在需要的时候, Redis 会将整数转换成浮点数.整数的取值 ...
- 三十二:WEB漏洞-文件操作之文件下载读取全解
文件下载读取 原路,检测,利用,修复 利用 数据库配置文件下载或者读取后续 接口密钥信息文件下载或者读取后续 文件名,参数值,目录符号 read.xxx?filename= down.xxx?file ...