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,和源点连边,容量为原 ...
随机推荐
- 聊聊WS-Federation(test)
本文来自网易云社区 单点登录(Single Sign On),简称为 SSO,目前已经被大家所熟知.简单的说, 就是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. 举例: 我们 ...
- hdu1394Minimum Inversion Number(线段树,求最小逆序数)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- 文件包含漏洞(RFI)
1文件包含漏洞简介 include require include_once require_once RFI综述 RFI是Remote File Inclusion的英文缩写,直译过来就是远 ...
- RabbitMQ基础教程之Spring&JavaConfig使用篇
RabbitMQ基础教程之Spring使用篇 相关博文,推荐查看: RabbitMq基础教程之安装与测试 RabbitMq基础教程之基本概念 RabbitMQ基础教程之基本使用篇 RabbitMQ基础 ...
- 安装SQLSEVER与MySQL
昨天装了一整填的SQLSEVER,今天是把昨天遗留的问题给重新整合一下,上午安装MySQL的时候,是在网上找的帖子通过压缩包安装的,捣鼓了一上午,下午花不到一个小时, 去安装好了,我觉得通过压缩包安装 ...
- Python2快速入门教程,只需要这十五张图片就够了!
今天给大家分享的教程是适用于Python 2.7,但它可能适用于Python 2.Python 2.7将停止在2020中的支持. 与Python 2.7和3兼容的Python代码是完全可能的.通过使用 ...
- struts2源码分析-初始化流程
这一篇文章主要是记录struts.xml的初始化,还原struts2.xml的初始化流程.源码依据struts2-2.3.16.3版本. struts2初始化入口,位于web.xml中: <fi ...
- leetcode7_C++整数反转
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 输出: 示例 2: 输入: - 输出: - 示例 3: 输入: 输出: 注意: 假设我们的环境只能存 ...
- C++ 学习笔记之——STL 库 vector
vector 是一种顺序容器,可以看作是可以改变大小的数组. 就像数组一样,vector 占用连续的内存地址来存储元素,因此可以像数组一样用偏移量来随机访问,但是它的大小可以动态改变,容器会自动处理内 ...
- 重装win10后ubuntu引导项修复
问题描述:原来是在win7下装了ubuntu14的双系统,后台win7换win10,然后使用EasyBCD进行引导项修复时,不好使,报 error file: /boot/grub/i386-pc/n ...