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 ...
随机推荐
- linux环境下oracle 11g 静默安装
安装环境 Linux服务器:oracle linux 6.6 64位 Oracle服务器:Oracle11gR2 64位 系统要求 1.Linux安装Oracle系统要求 系统要求 说明 内存 必须高 ...
- git的基础知识
git 分布式版本控制工具 具备的功能 协同开发 多人并行不悖修改服务器端的同一个文件 数据备份 不仅保持目录和文件当前状态,还能保存每一个提交的历史版本 版本管理 保存每一个版本的文件信息的时候做到 ...
- [C#.NET 拾遗补漏]14:使用结构体实现共用体
在 C 和 C# 编程语言中,结构体(Struct)是值类型数据结构,它使得一个单一变量可以存储多种类型的相关数据.在 C 语言中还有一种和结构体非常类似的语法,叫共用体(Union),有时也被直译为 ...
- maven 报的一堆错
今天初学maven,刚开始下载的是Apache-maven-3.6.2然后配置运行一个servlet,但是在pom.xml中写jar包坐标时一直报错显示红色,本地仓库和官网上的中央仓库都试过了就是依赖 ...
- MySQL where 条件字句查询
where 条件字句 搜索条件可由一个或多个逻辑表达式组成 , 结果一般为布尔值 逻辑运算符 运算符 语法 描述 and && a and b a && b 逻辑与 两 ...
- 解压rpm文件
rpm2cpio zabbix-2.2.2-0.el6.zbx.src.rpm |cpio -div
- bash shell关联数组总结
[原创]本博文为原创博文,引用或转发请注明原始出处和链接:https://www.cnblogs.com/dingbj/p/dict_array.html 什么是关联数组? 关联数组相对于索引数组,又 ...
- CTF实验吧-WEB题目解题笔记(1)简单的登陆题
1.简单的登陆题 解题链接: http://ctf5.shiyanbar.com/web/jiandan/index.php Burp抓包解密 乱码,更换思路.尝试id intruder 似乎也没什 ...
- GitLab-CI/CD入门实操
以Spring boot项目为例.传统方式是本地生成jar包,FTP上传服务器,重启服务:如果是内网测试服,也可以在服务器上安装git,在服务器上编译打包.但这都需要人为干预,于是CI/CD就出现了. ...
- Redisson 分布式锁实战与 watch dog 机制解读
Redisson 分布式锁实战与 watch dog 机制解读 目录 Redisson 分布式锁实战与 watch dog 机制解读 背景 普通的 Redis 分布式锁的缺陷 Redisson 提供的 ...