[Codeforces 946G]Almost Increasing Array
Description
给你一个长度为 \(n\) 的序列 \(A\) 。现在准许你删除任意一个数,删除之后需要修改最小的次数使序列单调递增。问最小次数。
\(1\leq n\leq 200000\)
Solution
注意到如果不要删除一个数的话,显然这就是一个模型问题了。只需要求出序列中每个数减去其位权,求出最长单调不下降子序列长度 \(len\) 。答案就是 \(n-len\) 。
但要删除一个数的话就不好直接做了,考虑问题出在了哪里。
因为删除一个数后,这个数之后的所有位权都 \(-1\) 。即实际上是对于删除的这个数,相当于断点,断点前每个数减去位权,断点后每个数减去(位权 \(-1\) )。再用相同的方法处理。
令 \(f_{i,1/0}\) 表示第 \(i\) 位及以前是否进行过“删数”操作,得到的最长不降序列长度。依旧可以用二分优化。
Code
//It is made by Awson on 2018.3.12
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 200000, INF = ~0u>>1;
void read(int &x) {
char ch; bool flag = 0;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
x *= 1-2*flag;
}
void print(int x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(int x) {if (x < 0) putchar('-'); print(Abs(x)); }
int n, f[N+5], g[N+5], ans, t, a[N+5];
void work() {
read(n);
for (int i = 1; i <= n; i++) read(a[i]), f[i] = g[i] = INF; f[0] = g[0] = -INF;
for (int i = 2; i <= n; i++) {
t = upper_bound(f+1, f+n+1, a[i]-i+1)-f; ans = Max(ans, t); f[t] = a[i]-i+1;
t = upper_bound(g+1, g+n+1, a[i-1]-i+1)-g; ans = Max(ans, t); g[t] = a[i-1]-i+1, f[t] = Min(f[t], g[t]);
}
writeln(n-1-ans);
}
int main() {
work(); return 0;
}
[Codeforces 946G]Almost Increasing Array的更多相关文章
- Codeforces 946G Almost Increasing Array (树状数组优化DP)
题目链接 Educational Codeforces Round 39 Problem G 题意 给定一个序列,求把他变成Almost Increasing Array需要改变的最小元素个数. ...
- codefroces 946G Almost Increasing Array
Description给你一个长度为$n$的序列$A$.现在准许你删除任意一个数,删除之后需要修改最小的次数使序列单调递增.问最小次数.$1≤n≤200000$ExamplesInput55 4 3 ...
- Codeforces 442C Artem and Array(stack+贪婪)
题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...
- Codeforces Round #504 D. Array Restoration
Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...
- Almost Increasing Array CodeForces - 946G (dp)
大意: 定义几乎递增序列为删除不超过一个数后序列严格递增. 给定序列, 求最少改变多少个数能变为几乎递增序列. 跟hdu5256类似,
- Educational Codeforces Round 21 D.Array Division(二分)
D. Array Division time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- Codeforces 754A Lesha and array splitting(简单贪心)
A. Lesha and array splitting time limit per test:2 seconds memory limit per test:256 megabytes input ...
- Codeforces 408 E. Curious Array
$ >Codeforces \space 408 E. Curious Array<$ 题目大意 : 有一个长度为 \(n\) 的序列 \(a\) ,\(m\) 次操作,每一次操作给出 \ ...
- Educational Codeforces Round 11A. Co-prime Array 数学
地址:http://codeforces.com/contest/660/problem/A 题目: A. Co-prime Array time limit per test 1 second me ...
随机推荐
- 【Java EE】从零开始写项目【总结】
从零开发项目概述 最近这一直在复习数据结构和算法,也就是前面发出去的排序算法八大基础排序总结,Java实现单向链表,栈和队列就是这么简单,十道简单算法题等等... 被虐得不要不要的,即使是非常简单有时 ...
- 记录python接口自动化测试--pycharm执行测试用例时需要使用的姿势(解决if __name__ == "__main__":里面的程序不生效的问题)(第三目)
1.只运行某一条case 把光标移动到某一条case后面,然后右键,选择"Run..."来运行程序 此时,pycharm会只运行光标所在位置的这一条case 2.如果想执行全部ca ...
- 201621123031 《Java程序设计》第8周学习总结
作业08-集合 1.本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2.书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码 ...
- xapp1151_Param_CAM模块安装
xapp1151_Param_CAM模块安装 所需生成模块 TCAM CAM 下载链接 赛灵思技术支持网站:http://www.xilinx.com/support.html 并在网页中搜索xapp ...
- nyoj 星期几?
星期几? 时间限制:500 ms | 内存限制:65535 KB 难度:2 描述 Acmer 小鱼儿 埋头ku算一道题 条件:已知给定 一日期 告诉你 ...
- Python内置函数(14)——bytes
英文文档: class bytes([source[, encoding[, errors]]]) Return a new "bytes" object, which is an ...
- Python内置函数(13)——bytearray
英文文档: class bytearray([source[, encoding[, errors]]]) Return a new array of bytes. The bytearray cla ...
- iot前台开发环境:请求示例
参考链接:http://www.cnblogs.com/keatkeat/category/872790.html 编辑->update保存 一.typescipt import { Injec ...
- SpringCloud的Hystrix(一) 一个消费者内的两个服务监控
一.概念与定义 1.服务雪崩 在微服务架构中,整个系统按业务拆分出一个个服务,这些服务之间可以相互调用(RPC),为了保证服务的高可用,单个服务通常会集群部署. 但是由于网络原因或自身原因,服务并不能 ...
- Spring Security入门(2-2)Spring Security 的运行原理 2