IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> [思维][双指针]Klee in Solitary Confinement 2021年ICPC南京站C -> 正文阅读

[C++知识库][思维][双指针]Klee in Solitary Confinement 2021年ICPC南京站C

Since the traveler comes, People in Monstadt suddenly raise great interest in computer programming and algorithms, including Klee, the Spark Knight of the Knights of Favonius.

Source: Genshin Impact Official

Being sent to solitary confinement by Jean again, Klee decides to spend time learning the famous Mo's algorithm, which can compute with a time complexity of?O(n1.5)O(n1.5)?for some range query problem without modifications.

To check whether Klee has truly mastered the algorithm (or in fact making another bombs secretly), Jean gives her a problem of an integer sequence?a1,a2,?,ana1,a2,?,an?along with some queries?[li,ri][li,ri]?requiring her to find the mode number in the contiguous subsequence?ali,ali+1,?,ariali,ali+1,?,ari. The mode number is the most common number (that is to say, the number which appears the maximum number of times) in the subsequence.

With the help of Mo's algorithm, Klee solves that problem without effort, but another problem comes into her mind. Given an integer sequence?a1,a2,?,ana1,a2,?,an?of length?nn?and an integer?kk, you can perform the following operation at most once: Choose two integers?ll?and?rr?such that?1≤l≤r≤n1≤l≤r≤n?and add?kk?to every?aiai?where?l≤i≤rl≤i≤r. Note that it is OK not to perform this operation. Compute the maximum occurrence of the mode number of the whole sequence if you choose to perform (or not perform) the operation optimally.

Input

There is only one test case in each test file.

The first line of the input contains two integers?nn?and?kk?(1≤n≤1061≤n≤106,??106≤k≤106?106≤k≤106) indicating the length of the sequence and the additive number.

The second line of the input contains?nn?integers?a1,a2,?,ana1,a2,?,an?(?106≤ai≤106?106≤ai≤106) indicating the original sequence.

Output

Output one line containing one integer indicating the maximum occurrence of the mode number of the whole sequence after performing (or not performing) the operation.

Examples

input

5 2
2 2 4 4 4

output

5

input

7 1
3 2 3 2 2 2 3

output

6

input

7 1
2 3 2 3 2 3 3

output?

5

input

9 -100
-1 -2 1 2 -1 -2 1 -2 1

output

3

题意:?有一个长度为n的数组a,以及一个参数k,现在要求整个数组中出现次数最多的那个数的出现次数。你可以进行1次或0次操作,操作内容是任选一个区间[l, r]并让每个数+k,问最终答案是多少。

分析:?可以对原数组中每个数字存储下来它出现的位置,方便起见用vector数组存储,a[i]表示i这个数字在原数组中出现在全部位置,然后枚举原数组中的每个数i,求出i+k的最大出现次数,具体过程是一个双指针,一个指针维护i出现的位置,一个指针维护i+k出现的位置,一开始i+k出现次数一定是a[i+k].size(),设num为i+k出现次数,那么初始时num = a[i+k].size(),之后按照原数组中先后顺序移动双指针,当遇到i时num++并记录最大出现次数,当遇到i+k时num--并且num不能减到小于a[i+k].size(),因为即使不操作也能获得a[i+k].size()个i+k,当扫描完全部数字后就得到了答案,总时间复杂度为O(n)。最后需要注意n == 1的情况和k == 0的情况。

具体代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;

vector<int> a[3000005];

signed main()
{
	int n, k;
	scanf("%d%d", &n ,&k);
	for(int i = 1; i <= n; i++){
		int t;
		scanf("%d", &t);
		t += 2e6+1;
		a[t].push_back(i);
	}	
	int ans = 0;
	for(int i = 1; i <= 3000001; i++){
		if(a[i].size() && k != 0){
			int mx = 0;
			if(i+k <= 3000001 && a[i+k].size() != 0){
				int num = a[i+k].size();
				int x = 0, y = 0;
				while(x < a[i].size() || y < a[i+k].size()){
					if(x == a[i].size()){
						y++;
					}
					else if(y == a[i+k].size()){
						x++;
						num++;
						mx = max(num, mx);
					}
					else if(a[i][x] < a[i+k][y]){
						x++;
						num++; 
						mx = max(num, mx);
					}
					else{
						if(a[i+k][y] > a[i][0]){
							num--;
							num = max(num, (int)a[i+k].size()); 
						}
						y++;
					}
				}
				mx = max(num, mx);
			}
			ans = max(ans, mx);
		}
		ans = max(ans, (int)a[i].size());
	}
	printf("%d\n", ans);
	return 0;
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-11-05 00:06:35  更:2022-11-05 00:11:36 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 4:02:31-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码