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

CF1082E:E.increasing Frequency(贪心&最大连续和)的更多相关文章

  1. CF 1082E Increasing Frequency(贪心)

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

  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 Increasing Frequency (multiset+乱搞+贪心)

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

  4. [CF1082E] Increasing Frequency

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

  5. [LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...

  6. Codeforces Beta Round #11 A. Increasing Sequence 贪心

    A. Increasing Sequence 题目连接: http://www.codeforces.com/contest/11/problem/A Description A sequence a ...

  7. 674. Longest Continuous Increasing Subsequence最长连续递增子数组

    [抄题]: Given an unsorted array of integers, find the length of longest continuous increasing subseque ...

  8. LeetCode 674. Longest Continuous Increasing Subsequence最长连续递增序列 (C++/Java)

    题目: Given an unsorted array of integers, find the length of longest continuous increasing subsequenc ...

  9. [LeetCode] 674. Longest Continuous Increasing Subsequence 最长连续递增序列

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...

随机推荐

  1. java程序运行一段时间之后停止

    原创文章,未经作者允许,禁止转载!!!!!!! 如何用java是一段代码运行一段时间之后自动停止运行? 就拿打印随机函数的代码来做例子吧,让程序随机打印1-10的数字,打印十秒钟后停止打印: publ ...

  2. cocos代码研究(21)Widget子类Text,TextAtlas,TextBMFont学习笔记

    理论基础 Text类又称ttf格式文本,可以用ttf文件或者系统自带字体,支持文字多,但是ttf文件格式体积大,渲染速度慢: TextBMFont类又称fnt格式文本,纹理创建,根据纹理上有的文字来显 ...

  3. EditPlus 4.3.2477 中文版已经发布(11月3日更新)

    新的版本修复了之前版本文本库和自动完成功能中的“^!”符号在填充项前面时不能正常工作的问题.

  4. smarty简单语法

    什么是smarty及其安装 Smarty是一个php模板引擎,它分开了逻辑程序和外在的内容,提供了一种易于管理的方法. Smarty要求web服务器运行php4.0.6和以上版本. smarty安装需 ...

  5. 反射_IsDefined判断方法上有自定义的标签

    在.NET 4.0(当然也包括4.0以前的版本)下,用反射判断某个方法是否运用了自定义Attribute时,可以通过调用MethodInfo的IsDefined()方法进行确认.当然,IsDefine ...

  6. c++第二十天

    p111~p114: 与旧代码的接口 1.C++程序中的C风格字符串:任何出现字符串字面值的地方都可以以空字符结束的字符数组来替代. const char str[] = {"Hello, ...

  7. 使用cronolog工具给tomcat进行日志切割

    关于cronolog的用法查看:https://www.freebsd.org/cgi/man.cgi?query=cronolog&apropos=0&sektion=0&m ...

  8. ubuntu16.04解决tensorflow提示未编译使用SSE3、SSE4.1、SSE4.2、AVX、AVX2、FMA的问题【转】

    本文转载自:https://blog.csdn.net/Nicholas_Wong/article/details/70215127 rticle/details/70215127 在我的机器上出现的 ...

  9. AngularJs 控制台

    在控制台查看$scope对象 html: 通过控制器里面的一个元素来获取这个控制器的$scope var node=document.getElementById("NewsVote&quo ...

  10. When should I use OWIN Katana?

    When should I use OWIN Katana? 解答1 In asp.net WebApi v2, the OWIN pipeline becomes the default. It i ...