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的更多相关文章

  1. #分组背包 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 ...

  2. 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 开始 ...

  3. 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 取模也是一样的,就当多减几次. 在欧几里得最初的 ...

  4. 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 ...

  5. Educational Codeforces Round 58 (Rated for Div. 2) G 线性基

    https://codeforces.com/contest/1101/problem/G 题意 一个有n个数字的数组a[],将区间分成尽可能多段,使得段之间的相互组合异或和不等于零 题解 根据线性基 ...

  6. 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即可.(感 ...

  7. Educational Codeforces Round 51 (Rated for Div. 2) G. Distinctification(线段树合并 + 并查集)

    题意 给出一个长度为 \(n\) 序列 , 每个位置有 \(a_i , b_i\) 两个参数 , \(b_i\) 互不相同 ,你可以进行任意次如下的两种操作 : 若存在 \(j \not = i\) ...

  8. 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, ...

  9. Educational Codeforces Round 47 (Rated for Div. 2)G. Allowed Letters 网络流

    题意:给你一个字符串,和每个位置可能的字符(没有就可以放任意字符)要求一个排列使得每个位置的字符在可能的字符中,求字典序最小的那个 题解:很容易判断有没有解,建6个点表示从a-f,和源点连边,容量为原 ...

随机推荐

  1. ruby 基础教程1-8-1

    1.":class, instance_of?, :is_a?"都是Object类的方法,每个对象都可以调用 2.":class"方法用户获取对象归属类的名称 ...

  2. 通过 Python_Faker 生成测试数据

    通过 Python_Faker 生成测试数据 一.介绍 在软件需求.开发.测试过程中,有时候需要使用一些测试数据,针对这种情况,我们一般要么使用已有的系统数据,你不可能通过手工来生成(最傻的方法)可能 ...

  3. TPO-11 C2 Work for the biology committee

    committee 委员会 representative 代表 department secretary 系里的秘书 applicant 申请人 TPO-11 C2 Work for the biol ...

  4. Spring Cloud(五):Hystrix 监控面板【Finchley 版】

    Spring Cloud(五):Hystrix 监控面板[Finchley 版]  发表于 2018-04-16 |  更新于 2018-05-10 |  在上一篇 Hystrix 的介绍中,我们提到 ...

  5. python中的迭代器与生成器

    迭代器 迭代器的引入 假如我现在有一个列表l=['a','b','c','d','e'],我想取列表中的内容,那么有几种方式? 1.通过索引取值 ,如了l[0],l[1] 2.通过for循环取值 fo ...

  6. 初步了解CUDA(零)

    初步了解CUDA,从历史开始,先不开发:

  7. UVa 1583 - Digit Generator 解题报告 - C语言

    1.题目大意 如果a加上a的各个数字之和得到b,则说a是b的生成元.给出n其中$1\le n\le 100000$,求其最小生成元,若没有解则输出0. 2.思路 使用打表的方法打出各个数字a对应的b, ...

  8. "Hello world!"团队第一次会议

    今天是我们"Hello world!"团队第一次召开会议,今天的会议可能没有那么正式,但是我们一起确立了选题——基于WEB的售票系统.博客内容是: 1.会议时间 2.会议成员 3. ...

  9. block知识总结

    一.block在内存中存在的形式 1.当把block句法写在函数或者方法外面时,系统会在静态数据区分配一块内存区域给block对象.这片区域在程序执行期会一直存在. 2.当block句法写在函数或者方 ...

  10. scrapy(2)——scrapy爬取新浪微博(单机版)

    Sina爬虫教程   Scrapy环境搭建   环境:window10 + python2.7(包含scrapy)+ mongoDB 1.1 安装集成了python2.7的anaconda   ana ...