Description

给定一个长度为 \(n\) 的数列 \(a\) ,你可以任意选择一个区间 \([l,r]\) ,并给区间每个数加上一个整数 \(k\) ,求这样一次操作之后数列中最多有多少个数等于 \(c\)。

\(n,c,a_i\leq 10^5\)

Solution

假设当前选择区间的右端点为 \(r\),那我们要强制将 \(a_r\) 这个元素变为 \(c\),不然可以通过让右端点 \(-1\) 使答案变得不劣。

同理,如果我们左端点 \(l\) 的元素 \(a_l\) 也要让其强制等于 \(a_r\),不然同样可以通过让左端点 \(+1\) 使答案变的不劣。

所以这个性质告诉我们选中的区间 \([l,r]\) 一定满足 \(a_l=a_r\)。

这启发我们对于每种取值分开做

设 \(f(x)\) 是右端点为 \(x\) 时最多有多少个元素取到 \(c\) 。

考虑列出 \(DP\) 式

\(f(x)=\max \left\{pre(las-1)+count(las,x)+suf(x+1)\right\}\),其中 \(pre(i)\) 表示从 \(1\sim i\) 有多少个元素为 \(c\),同理 \(suf(i)\) 表示从 \(i\sim n\) 有多少个元素为 \(c\), \(count(l,r)\) 表示在区间 \([l,r]\) 里有多少个元素等于这个区间的端点即 \(a_r\)。

发现如果拿 \(vector\) 存的话这是一个单调队列优化 \(DP\) 的标准式子

随便优化一下就吼了

Code

#include<bits/stdc++.h>
using std::min;
using std::max;
using std::swap;
using std::vector;
typedef double db;
typedef long long ll;
#define pb(A) push_back(A)
#define pii std::pair<int,int>
#define all(A) A.begin(),A.end()
#define mp(A,B) std::make_pair(A,B)
const int N=5e5+5; vector<int> v[N];
int n,m,val[N],vis[N],q[N],hd;
int pre[N],suf[N],tot[N],tail; int getint(){
int X=0,w=0;char ch=getchar();
while(!isdigit(ch))w|=ch=='-',ch=getchar();
while( isdigit(ch))X=X*10+ch-48,ch=getchar();
if(w) return -X;return X;
} signed main(){
n=getint(),m=getint();
for(int i=1;i<=n;i++)
val[i]=getint(),pre[i]=pre[i-1]+(val[i]==m),v[val[i]].pb(i);
for(int i=n;i;i--)
suf[i]=suf[i+1]+(val[i]==m);
int ans=0;
for(int i=1;i<=n;i++){
if(!vis[val[i]]){
vis[val[i]]=1;
hd=1,tail=0;
for(int j=0;j<v[val[i]].size();j++){
while(hd<=tail and pre[ v[ val[i] ][ j ] -1 ]-j>=pre[ v[ val[i] ][ q[tail] ] -1 ]-q[tail]) tail--;
q[++tail]=j;
if(hd<=tail) ans=max(ans,pre[ v[ val[i] ][ q[hd] ] -1 ]+j-q[hd]+1+suf[ v[ val[i] ][ j ] +1 ]);
}
}
} printf("%d\n",ans);
return 0;
}

[CF1082E] Increasing Frequency的更多相关文章

  1. CF1082E Increasing Frequency (multiset+乱搞+贪心)

    题目大意: \(给你n个数a_i,给定一个m,你可以选择一个区间[l,r],让他们区间加一个任意数,然后询问一次操作完之后,最多能得到多少个m\) QWQ 考场上真的** 想了好久都不会,直到考试快结 ...

  2. Educational Codeforces Round 55 (Rated for Div. 2):E. Increasing Frequency

    E. Increasing Frequency 题目链接:https://codeforces.com/contest/1082/problem/E 题意: 给出n个数以及一个c,现在可以对一个区间上 ...

  3. CF1082E:E.increasing Frequency(贪心&最大连续和)

    You are given array a a of length n n . You can choose one segment [l,r] [l,r] (1≤l≤r≤n 1≤l≤r≤n ) an ...

  4. Codeforces.1082E.Increasing Frequency(思路)

    题目链接 \(Description\) 给定\(n\)个数.你可以选择一段区间将它们都加上或减去任意一个数.求最终序列中最多能有多少个数等于给定的\(C\). \(n\leq5\times10^5\ ...

  5. CodeForces 1082 E Increasing Frequency

    题目传送门 题意:给你n个数和一个c, 现在有一个操作可以使得 [ l, r ]区间里的所有数都加上某一个值, 现在问你c最多可以是多少. 题解: pre[i] 代表的是 [1,i] 中 c 的个数是 ...

  6. CF 1082E Increasing Frequency(贪心)

    传送门 解题思路 贪心.对于一段区间中,可以将这段区间中相同的元素同时变成\(c\),但要付出的代价是区间中等于\(c\)的数的个数,设\(sum[i]\)表示等于\(c\)数字的前缀和,Max[i] ...

  7. CF1082解题报告

    CF1082A Vasya and Book 模拟一下即可 \(Code\ Below:\) #include <bits/stdc++.h> using namespace std; c ...

  8. UVA 11248 Frequency Hopping

    Frequency Hopping Time Limit: 10000ms Memory Limit: 131072KB This problem will be judged on UVA. Ori ...

  9. 纠错工具之 - Proovread

    BioInf-Wuerzburg/proovread - Github 主要是来解读 proovread 发表的文章,搞清楚它内在的原理. Proovread,这个工具绝对没有你想的那么简单,它引入了 ...

随机推荐

  1. 20170805_Elasticsearch_简介和安装

    官网地址: https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started.html Elk 是一个可高 ...

  2. django by example 第四章 扩展 User 模型( model)

    描述: RelatedObjectDoesNotExist at /account/edit/ User has no profile.​ 原因: 注意原书,要求新建一个账户才能使用.

  3. 可以用到的XSS跨站语句

    我们常用的测试XSS跨站的语句一般是alert比如: <script>alert(“sex”)</script> <script>alert(/sex/)</ ...

  4. ios下面的按钮和inout框

    在ios系统中,按钮和输入框,会默认给你加一个圆角和阴影,可以用css去掉这个自带的属性 input[type=button], input[type=submit], input[type=file ...

  5. explain 类型分析

    all 全表扫描 ☆ index 索引全扫描 ☆☆ range 索引范围扫描,常用语<,<=,>=,between等操作 ☆☆☆ ref 使用非唯一索引扫描或唯一索引前缀扫描,返回单 ...

  6. Python数字与字符之间的转换

    Python数字与字符之间的转换 命令 意义 int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 float(x ) 将x转换到一个浮点数 co ...

  7. 三.mysql表的完整性约束

    mysql表的完整性约束 什么是约束 not null    不能为空的 unique      唯一 = 不能重复 primary key 主键 = 不能为空 且 不能重复 foreign key ...

  8. Django View 进阶

    返回404 from django.http import HttpResponse, HttpResponseNotFound def not_found(request): ) 或 return ...

  9. iOS-关于缓存【SDImageCache】Image,一直刷新UIImageView内存一直增加问题

    最近做的一个项目,里面有这样一个需求,在一个页面,用一个UIImageView不停的刷新显示图片,图片可能会重复显示:图片是从服务器下载下来的data流,data转UIimage系统的方法: UIIm ...

  10. 个人博客搭建( wordpress )

    最近同学买了一台虚机( centos7 系统 ).一个域名,让帮忙搭一个个人博客.本着简单快捷,个人博客采用 wordpress 来搭建.为了以后博客系统的迁移方便,使用 docker 来安装 wor ...