E. Increasing Frequency

题目链接:https://codeforces.com/contest/1082/problem/E

题意:

给出n个数以及一个c,现在可以对一个区间上的数同时加上或减去一个值,问最后c的最多数量为多少。

题解:

这题挺有意思的,我们通过分析题目可以发现,对于每一个d,如果把[l,r]里面的d都变为c,则最后c的数量为cnt(c,1,l-1)+cnt(c,r+1,n)+cnt(d,l,r)。

这个式子变化一下,有:cnt(c,1,n)+cnt(d,l,r)-cnt(c,l,r)。

现在就只需要维护cnt(d,l,r)-cnt(c,l,r)的最大值就可以了。

这里有许多种维护方式,我说下我用的。

假设a[x1]=a[x2]=...a[xn]=d,那么对于[x1+1,x2-1]...[xn+1,n]这些区间,是没有d的,则我们需要维护的值就变为了-cnt(c,l,r),这里就相当于把连续的区间缩成了一个点。

为什么能缩点?我理解的就是选择的区间是一段连续的区间,缩点后的每个点,只要把当前的点加上的和为正(此时把当前连续的区间选完)就可以选择这一个点,否则就直接以下一个点为起点(当前区间就一个不选,之前已经保存了一个最大值了)。

所以对于一段连续的没有d的区间,要么选择连续的一段区间,要么直接不选,所以缩点后,用最大连续子段和的技巧就可以了~

代码如下:

#include <bits/stdc++.h>
using namespace std; const int N = 1e6 ;
int a[N];
int cnt[N];
int n,c,ans=-;
vector <int> pos[N];
int calc(int x){
if(x==c) return ;
vector <int> vec;
int len = pos[x].size();
if(len<=) return ;
vec.push_back(-cnt[pos[x][]-]);
vec.push_back();
for(int i=;i<len;i++){
vec.push_back(-cnt[pos[x][i]-]+cnt[pos[x][i-]]);
vec.push_back();
if(i==len-) vec.push_back(-cnt[n]+cnt[pos[x][i]]);
}
int tot = ,sum = ,l = vec.size();
for(int i=;i<l;i++){
sum+=vec[i];
if(sum>=tot){
tot=sum;
}else if(sum<) sum=;
}
return max(tot,sum);
}
int main(){
scanf("%d%d",&n,&c);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
cnt[i]=cnt[i-]+(a[i]==c);
pos[a[i]].push_back(i);
}
for(int i=;i<=N-;i++){
ans=max(calc(i)+cnt[n],ans);
}
printf("%d",ans);
return ;
}

Educational Codeforces Round 55 (Rated for Div. 2):E. Increasing Frequency的更多相关文章

  1. Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph

    D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...

  2. Educational Codeforces Round 55 (Rated for Div. 2):C. Multi-Subject Competition

    C. Multi-Subject Competition 题目链接:https://codeforces.com/contest/1082/problem/C 题意: 给出n个信息,每个信息包含专业编 ...

  3. Educational Codeforces Round 55 (Rated for Div. 2)E

    题:https://codeforces.com/contest/1082/problem/E 题意:给出n个数和一个数c,只能操作一次将[L,R]之间的数+任意数,问最后该序列中能存在最多多少个c ...

  4. Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition 【vector 预处理优化】

    传送门:http://codeforces.com/contest/1082/problem/C C. Multi-Subject Competition time limit per test 2 ...

  5. Educational Codeforces Round 55 (Rated for Div. 2) A/B/C/D

    http://codeforces.com/contest/1082/problem/A WA数发,因为默认为x<y = = 分情况讨论,直达 or x->1->y  or  x-& ...

  6. Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies 【贪心 】

    传送门:http://codeforces.com/contest/1082/problem/B B. Vova and Trophies time limit per test 2 seconds ...

  7. Codeforces 1082 C. Multi-Subject Competition-有点意思 (Educational Codeforces Round 55 (Rated for Div. 2))

    C. Multi-Subject Competition time limit per test 2 seconds memory limit per test 256 megabytes input ...

  8. Codeforces 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))

    A. Vasya and Book time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  9. Educational Codeforces Round 55 (Rated for Div. 2)

    D. Maximum Diameter Graph 题意 给出每个点的最大度,构造直径尽可能长的树 思路 让度数大于$1$的点构成链,考虑是否能在链的两端加度为$1$的点 代码 #include &l ...

随机推荐

  1. python基础知识 -- set集合

    Set集合:是Python的一个基本数据类型.一般不是很常用.Set中的元素是不重复的,无序的,里面的元素必须是可hash的(int,str,tuple,bool).我们可以这样来计Set就是dict ...

  2. linux文件操作篇 (二) 打开和关闭文件

    2.1 打开文件和关闭文件 #include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>  头文件 i ...

  3. node获取头信息数据

    req.fresh req.stale var version = 100; app.get('/test',function(req,res){ res.set('etag',version); i ...

  4. 【UE4】二十六、Look at camera 蓝图

    如图,把BP_Cube替换为你需要的对象(如3DUI等)即可.

  5. 使用maven插件生成grpc所需要的Java代码

    1.首先需要编写自己需要的.proto文件,本文重点不在这里,.proto可以参考grpc官方例子 https://grpc.io/docs/quickstart/java.html 2.创建自己的J ...

  6. 关于transition动画下,如果有fixed元素,渲染的奇葩问题

    之前我们机票页面有生成一个低价日历,然后我们有一个需求就是滚动到那个月份,对应显示这个月,然后这个区域是fixed定位的,然后奇怪的事情发生了,就是低价日历的动画执行完后,修改页面的html却没有正常 ...

  7. 『AngularJS』Service

    理解Angular 服务 什么是Angular Service Angular 服务是为web应用执行特定任务的单例对象或方法. 注:如果组件是为了内容呈现的功能复用,那么服务就是为组件进行功能复用. ...

  8. Ubuntu下使用Git_2

    接着上一篇的写,这里练习一下git clone 指令 指令格式 $ git clone <repository> <directory> <respository> ...

  9. 在Kotlin编写RecyclerView适配器(KAD 16)

    作者:Antonio Leiva 时间:Mar 14, 2017 原文链接:https://antonioleiva.com/recyclerview-adapter-kotlin/ 通过创建Recy ...

  10. 问题 C: 质因数的个数

    1947: 质因数的个数 时间限制: 1 Sec  内存限制: 32 MB提交: 245  解决: 114[提交][状态][讨论版][命题人:外部导入] 题目描述 求正整数N(N>1)的质因数的 ...