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 ) and integer value k k (positive, negative or even zero) and change a l ,a l+1 ,…,a r  al,al+1,…,ar by k k each (i.e. a i :=a i +k ai:=ai+k for each l≤i≤r l≤i≤r ).

What is the maximum possible number of elements with value c c that can be obtained after one such operation?

Input

The first line contains two integers n n and c c (1≤n≤5⋅10 5  1≤n≤5⋅105 , 1≤c≤5⋅10 5  1≤c≤5⋅105 ) — the length of array and the value c c to obtain.

The second line contains n n integers a 1 ,a 2 ,…,a n  a1,a2,…,an (1≤a i ≤5⋅10 5  1≤ai≤5⋅105 ) — array a a .

Output

Print one integer — the maximum possible number of elements with value c c which can be obtained after performing operation described above.

Examples

Input
6 9
9 9 9 9 9 9
Output
6
Input
3 2
6 2 6
Output
2

题意:给定长度位N的序列,以及数字C,现在你可以给某个区间加一个数,使得最后这个序列的C个数最大,输出这个个数。

思路:假设我们在[L,R]这个区间加一个数(不为0),那么最后这个区间的贡献显然是众数减去这个区间原本为C的个数。

显然相同的数字一同考虑,考虑其贡献:每个数的贡献为1-到前一个的C的个数。

比如C=2; 那么1,2,3,2,1,5,第一个1的贡献是1,第二个1的贡献是-1,因为中间夹了两个C,即如果我们把1变为C,那么增加了一个C(1),但是减少了两个C(2)。

我们把每个数的贡献放到对应的数组里,然后就求“最大连续区间和”res。

而求最大连续区间和的过程,如果当前sum<1,舍去前一段,令sum=1,因为第一个pre是肯定可以做贡献为1的。 这里我wa了两发。其他地方还是蛮好想的。

下面的代码为为了避免负数,所有都加了一个Z=500000。

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int a[maxn],b[maxn],Laxt[maxn],ans,N,C,res;
vector<int>G[maxn];
int main()
{
scanf("%d%d",&N,&C); int Z=;
rep(i,,N) scanf("%d",&a[i]),a[i]=a[i]-C+Z;
rep(i,,N) {
b[i]=b[i-]; if(a[i]==Z) b[i]++;
int pre=Laxt[a[i]]; if(pre==) pre=i;
G[a[i]].push_back(-b[i]+b[pre]);
Laxt[a[i]]=i;
}
ans=b[N];
rep(i,,maxn-) {
if(i==Z) continue;
int tmp=;
for(int j=;j<G[i].size();j++){
tmp+=G[i][j];
res=max(tmp,res);
if(tmp<=) tmp=;
}
}
printf("%d\n",ans+res);
return ; }
//8 4 4 0 0 4 2 1 0 4
//12 1 1 0 1 0 1 1 1 0 0 1 1 0

最新文章

  1. mac+php+xdebug+phpstorm在苹果下配置xdebug一波三折
  2. 将file转变成contenthash
  3. NuGet学习笔记(2) 使用图形化界面打包自己的类库
  4. IOS第八天(6:UITableViewController新浪微博, 模型和 控件位置封装一起statusFrame)
  5. day12_API第二天
  6. .bat后台运行
  7. Epic - Spiral Matrix
  8. 小技巧之a标签自动解析URL
  9. Octave下载
  10. 【转】Myeclipse8.5 反编译插件 jad 安装
  11. CSS设置input placeholder文本的样式
  12. 2.2 Xpath-helper (chrome插件) 爬虫、网页分析解析辅助工具
  13. ubuntu14.04安装ssh和ftp
  14. chrome无法登陆账号,显示操作超时的解决方案
  15. C#与 微信小程序 互为加解密方案
  16. 论文类型Journal、magazin、transaction、letter等的区别
  17. CentOS双机中Docker下安装Mysql并配置互为主从模式
  18. 记账本微信小程序开发二
  19. 关于Django的Ajax操作
  20. Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2) D. Minimum path(字典序)

热门文章

  1. 2.6 The Object Model -- Bindings
  2. WordPress配置
  3. Least slack time scheduling
  4. Refactoring #002 Inline Method
  5. 关于《Java读书笔记》第六章课后习题选择题总结与疑问
  6. MVC中定时发布二维码邮件
  7. 【eclipse】运行maven项目clean tomcat7:run报错
  8. hiho一下 第二周 trie树
  9. 教你上传代码到码云(与github一样)
  10. windows下利用批处理命令生成maven项目(java、javaWeb)