Educational Codeforces Round 39 (Rated for Div. 2) G
Educational Codeforces Round 39 (Rated for Div. 2) G
题意:
给一个序列\(a_i(1 <= a_i <= 10^{9}),2 <= n <= 200000\), 如果至多删除其中的一个数之后该序列为严格上升序列,那么称原序列为几乎严格上升序列。
现在每次将序列中的任意数字变成任意数字,问最少要操作几次才能将序列变成几乎严格上升子序列。
思路:
如果不考虑删除,求让整个序列都变成严格上升子序列的次数
求出\(序列a_i - i\)的最长不下降子序列的长度\(len\), \(n - len\)就是答案
现在来考虑删除一个数的情况
我们枚举删除第\(k\)个数 前面的数做变换\(a_i - i (i < k)\), 后面的数做变换\(a_i - (i-1) (i > k)\)
我们计算以k-1结尾的最长不下降子序列和后面某个\(a_j(a_j >= a_{k-1})\)起始的最长不下降子序列拼接起来得到的长度,更新答案即可
先离散化处理
维护以\(a_i\)结尾的的最长不下降子序列的长度和以\(a_i\)开始的最长不下降子序列的长度
假设先从后往前处理,可以处理出后缀,同时也可以计算出每个数要拼接的最长后缀的长度,再从前往后处理算出前缀,更新答案即可。
用线段树维护 复杂度\(O(nlogn)\)
#include<bits/stdc++.h>
#define LL long long
#define P pair<int,int>
using namespace std;
const int N = 4e5 + 10;
int a[N];
vector<int> b;
int n,nn;
int dppre[N],dpsuf[N],sufmx[N];
int mx[N << 2];
void update(int pos,int val,int l,int r,int rt){
if(l == r){
mx[rt] = max(mx[rt],val);
return ;
}
int m = l + r >> 1;
if(pos <= m) update(pos,val,l,m,rt<<1);
else update(pos,val,m+1,r,rt<<1|1);
mx[rt] = max(mx[rt << 1], mx[rt << 1|1]);
}
int querymx(int L,int R,int l,int r,int rt){
if(L <= l && R >= r) return mx[rt];
int ans = 0;
int m = l + r>>1;
if(L <= m) ans = max(ans, querymx(L, R, l, m, rt<<1));
if(R > m) ans = max(ans, querymx(L, R, m + 1, r, rt<<1|1));
return ans;
}
/*
10
5 6 7 8 9 5 10 11 12 13
*/
int main(){
cin>>n;
for(int i = 1;i <= n;i++){
scanf("%d",a + i);
b.push_back(a[i] - i);
b.push_back(a[i] - i + 1);
}
sort(b.begin(), b.end());
b.erase(unique(b.begin(),b.end()),b.end());
nn = b.size();
for(int i = n;i >= 1;i--){
int pos = lower_bound(b.begin(), b.end(), a[i-1] - i+1) - b.begin() + 1;
if(i == 1) pos = 1;
sufmx[i-1] = querymx(pos, nn, 1, nn, 1);
pos = lower_bound(b.begin(), b.end(), a[i] - i + 1) - b.begin() + 1;
int v = querymx(pos, nn, 1, nn, 1);
dpsuf[i] = v + 1;
update(pos, dpsuf[i], 1, nn, 1);
//cout<<i - 1<<" "<<sufmx[i - 1]<<" "<<v + 1<<endl;
}
for(int i = 1;i <= (nn<<2);i++){
mx[i] = 0;
}
int ans = sufmx[0];
for(int i = 1;i <= n;i++){
int pos = lower_bound(b.begin(), b.end(), a[i] - i) - b.begin() + 1;
int v = querymx(1, pos, 1, nn, 1);
dppre[i] = v + 1;
ans = max(dppre[i] + sufmx[i], ans);
//cout<<i<<" "<<dppre[i]<<" "<<sufmx[i]<<endl;
update(pos, dppre[i], 1, nn, 1);
}
cout<<max(0,n - 1 - ans)<<endl;
return 0;
}
Educational Codeforces Round 39 (Rated for Div. 2) G的更多相关文章
- #分组背包 Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable
2018-03-11 http://codeforces.com/contest/946/problem/D D. Timetable time limit per test 2 seconds me ...
- Educational Codeforces Round 39 (Rated for Div. 2) 946E E. Largest Beautiful Number
题: OvO http://codeforces.com/contest/946/problem/E CF 946E 解: 记读入串为 s ,答案串为 ans,记读入串长度为 len,下标从 1 开始 ...
- Educational Codeforces Round 39 (Rated for Div. 2) B. Weird Subtraction Process[数论/欧几里得算法]
https://zh.wikipedia.org/wiki/%E8%BC%BE%E8%BD%89%E7%9B%B8%E9%99%A4%E6%B3%95 取模也是一样的,就当多减几次. 在欧几里得最初的 ...
- codeforces Educational Codeforces Round 39 (Rated for Div. 2) D
D. Timetable time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Educational Codeforces Round 58 (Rated for Div. 2) G 线性基
https://codeforces.com/contest/1101/problem/G 题意 一个有n个数字的数组a[],将区间分成尽可能多段,使得段之间的相互组合异或和不等于零 题解 根据线性基 ...
- Educational Codeforces Round 53 (Rated for Div. 2)G. Yet Another LCP Problem
题意:给串s,每次询问k个数a,l个数b,问a和b作为后缀的lcp的综合 题解:和bzoj3879类似,反向sam日神仙...lcp就是fail树上的lca.把点抠出来建虚树,然后在上面dp即可.(感 ...
- Educational Codeforces Round 51 (Rated for Div. 2) G. Distinctification(线段树合并 + 并查集)
题意 给出一个长度为 \(n\) 序列 , 每个位置有 \(a_i , b_i\) 两个参数 , \(b_i\) 互不相同 ,你可以进行任意次如下的两种操作 : 若存在 \(j \not = i\) ...
- Educational Codeforces Round 48 (Rated for Div. 2)G. Appropriate Team
题意:求满足条件的(i,j)对数:\(gcd(v,a_i)=x,lcm(v,a_j)=y\) 题解:\(x|a_i,a_j|y\),\(x|y\),考虑质因子p,假设a_i中p次数为a,x中次数为b, ...
- Educational Codeforces Round 47 (Rated for Div. 2)G. Allowed Letters 网络流
题意:给你一个字符串,和每个位置可能的字符(没有就可以放任意字符)要求一个排列使得每个位置的字符在可能的字符中,求字典序最小的那个 题解:很容易判断有没有解,建6个点表示从a-f,和源点连边,容量为原 ...
随机推荐
- uvaoj1586Molar mass(暴力)
An organic compound is any member of a large class of chemicalcompounds whose molecules contain carb ...
- hdu1159Common Subsequence(动态规划)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- RabbitMQ基础教程之Spring&JavaConfig使用篇
RabbitMQ基础教程之Spring使用篇 相关博文,推荐查看: RabbitMq基础教程之安装与测试 RabbitMq基础教程之基本概念 RabbitMQ基础教程之基本使用篇 RabbitMQ基础 ...
- 软件测试工程师必备的SQL语句基础
为一个软件测试工程师,我们在测试过程中往往需要对数据库数据进行操作,但是我们的操作大多以查询居多,有时会涉及到新增,修改,删除等操作,所以我们其实并不需要对数据库的操作有特别深入的了解,以下是我在工作 ...
- selenium,unittest——两个class连续运行
将多个class放在一个文件内一起运行,这是一个多用例不同网站进行测试的方法 #encoding=utf-8from selenium import webdriverimport time,unit ...
- 【SpringCloud】 第九篇: 服务链路追踪(Spring Cloud Sleuth)
前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...
- 【转】MMO即时战斗:技能实现
转自 http://blog.csdn.net/cyblueboy83/article/details/41628743 一.前言 基本所有MMO游戏无论是回合制.策略类.即时战斗等等类型都需要有相应 ...
- 解决jQuery不同版同时引用的冲突
今天研发的同事在开发一个新jQuery插件时,遇到一个揪心的问题.平台以前使用的 jQuery版本是1.2.6,偶,天啊!这是古代的版本啊! 由于很多功能基于老版本,不能删除啊,同志们都懂的! 于是我 ...
- [Clr via C#读书笔记]Cp16数组
Cp16数组 一维数组,多维数组,交错数组:引用类型:P338的图非常的清楚地描述了值类型和引用类型在托管堆中的关系:越界检查: 数组初始化 数组初始化器: 四种写法 string[] names = ...
- C++第一次课堂作业 circle
Github上的代码提交