Problem Description

You are given array a of length n. You can choose one segment [l,r] (1≤l≤r≤n) and integer value k (positive, negative or even zero) and change al,al+1,…,ar by k each (i.e. ai:=ai+k for each l≤i≤r).

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

Input

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

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

Output

Print one integer — the maximum possible number of elements with value cc 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,能将一段连续区间里的数都加上 k,使得整个序列中 c 尽可能的多,区间和 k 都由自己决定,求最多的个数

思路:学长说这个题是 DP,然后写到自闭。。。

一开始想的是枚举 1~L 与 R~n 的区间,计算将他们变为 c 的个数,当前面的 c 的数量和后面的 c 的都已确定,再统计将中间出现次数最多的数都变成 c 的个数,但是写到最后发现中间出现次数最多的数并不好计算。

于是,可以从 1 开始枚举到某处 i,再加上 i 之后的数列中 c 的个数

用两个数组 up[i]、down[i] 分别存储数列中从前向后、从后向前到 i 为止的等于 c 的个数

用 dp[i] 表示从前面某位置 pos 开始到现在位置 i,将 pos~i 之间出现次数最多的数变成 c、前 pos-1 个数最大的含 c 的数量

对于 pos 的位置,可以用一数组 pre[x]=j 存储,其表示对于某个数 a[i]==x,他上一次出现的位置是 j,即:a[j]=a[i]=x

从而有了状态转移方程:

dp[i]=max(up[i-1]+1,dp[pre[a[i]]]+1);
pre[a[i]]=i;
ans=max(ans,dp[i]+down[i+1]);

从而保证从某个位置 pos 开始,dp[pos+1]~dp[i] 这一段出现次数最多的数变成了 c 的最大的

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-6
#define MOD 16007
#define INF 0x3f3f3f3f
#define N 1000001
#define LL long long
using namespace std;
int a[N];
int up[N],down[N];
int dp[N];
int pre[N];
int main(){int n,c;cin>>n>>c;for(int i=1;i<=n;i++){cin>>a[i];up[i]=up[i-1]+(a[i]==c);}for(int i=n;i>=1;i--)down[i]=down[i+1]+(a[i]==c);int maxx=-INF;for(int i=1;i<=n;i++){dp[i]=max(up[i-1]+1,dp[pre[a[i]]]+1);pre[a[i]]=i;maxx=max(maxx,dp[i]+down[i+1]);}cout<<maxx<<endl;return 0;
}

Increasing Frequency(CF-1082E)相关推荐

  1. 【解题报告】随便练练二(CF 2300)

    [解题报告]随便练练二(CF 2300) A:Antimatter | CF383D 题意 思路 :DP 代码 B:Physical Education Lessons | CF915E 题意 思路一 ...

  2. Commentator problem(CF 2)

    题目链接 题目大意: 给定三个圆,询问是否存在点满足该点与三个圆夹角均相等,若存在多组解返回夹角最大值. 圆外一点到两圆夹角均相等: 即 sina = sinb = r1 / d1 = r2 / d2 ...

  3. cf 11A Increasing Sequence(水,)

    题意: A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i <  ...

  4. D. Make a Power of Two(cf#739DIV3)

    D. Make a Power of Two 链接: link. 题意: 找出将数字转换为 2 的任意幂的最小移动次数. 题解: 先将2的xxx次幂的结果以字符串形式保存,输入字符串nnn后,因为存在 ...

  5. Web of Lies(CF 1548A)

    这是今天在打个人赛时碰见的一道题,是一道半图论半思维的题. Web of Lies 题目大意不难理解,在这里只需要注意一些细节.在加边时,只有当cnt[min]的值为1时答案才应该减1,而不是当cnt ...

  6. Magic Powder - 2 (CF 670_D)

    http://codeforces.com/problemset/problem/670/D2 The term of this problem is the same as the previous ...

  7. 【解题报告】博弈专场 (CF 2000~2200)前五题

    [解题报告]博弈专场 (CF 2000+)前五题 A:Fox and Card Game | CF388C 题意 思路 代码 B:Berzerk | CF786A 题意 思路 代码 C:Ithea P ...

  8. 关于 minor allele frequency(次等位基因频率)的理解

    引用自NCBI的概念(https://www.ncbi.nlm.nih.gov/projects/SNP/docs/rs_attributes.html#gmaf) Global minor alle ...

  9. 软件设计师提纲+复习资料整理(上午题)

    文章目录 软件设计师考试大纲 上午题(选择题) 一.计算机组成原理 考点:CPU结构组成 考点:原码.反码.补码定点整数范围 考点:浮点数表示 考点:RISC和CISC计算机的区别 考点:奇校验与偶校 ...

最新文章

  1. 【老鸟分享】Linux命令行终端提示符多种实用技巧!
  2. MySQL Replace INTO的使用
  3. linux查看内核版本、系统版本、系统位数(32/64)
  4. 最优化课堂笔记04:非线性规划(考点4-5例题)
  5. 基于keepalived 实现VIP转移,lvs,nginx的高可用
  6. 常量(const)和只读变量(readonly)
  7. 技术人最不该忽视可视化数据分析!
  8. matlab画滤波器频响应,matlab如何画出“凯泽窗FIR滤波器”的幅频相频响应图
  9. LG WP7机型工程模式下越狱
  10. mysql 安装问题汇总_Windows 10 下MySQL安装及常见问题
  11. 【LeetCode】【数组】题号:56,重塑矩阵
  12. 关于Eclipse无法创建web项目的解决方案
  13. 基于TensorFlow的车牌号识别系统
  14. 一文读懂参考基因组和基因组注释+最全下载方法
  15. 如何免费使用xshell、xftp工具
  16. android 支付宝未安装,调用支付宝接口Android客户端没有支付宝APP的情况下解决无法调用支付宝页面的问题...
  17. 性能测试包括哪些方面?分类及测试方法有哪些?
  18. QTcpSocket使用过程中的一些问题记录
  19. linux安装mysql步骤用yum_linux 使用yum安装mysql详细步骤
  20. 【PAT B1015】德才论 (c语言)//答案正确

热门文章

  1. 谷歌、微软、亚马逊6个惊人的A/B测试实例
  2. 想宅家学习但实力不允许?9本书,揭秘学霸是如何养成的​
  3. linux宝塔登录不上去怎么回事,宝塔面板点击登陆没有用怎么办
  4. php类中引函数变量,一个非线性差分方程的隐函数解
  5. Java 18 新功能介绍
  6. 你还在代码里做读写分离么,试试这个中间件吧!
  7. 再现暴力裁员!患病员工被关小黑屋,摄像头监控,工作量超其他人!
  8. 微信网站-微信应用-微信二次开发-演示方案
  9. inittab文件剖析[CentOS 5.X](第二版)
  10. 构建模式--Adapter模式(JAVA)