Educational Codeforces Round 170 C New Game

思路

滑动窗口

排完序后找左右边界差值小于等于k 的最长子序列长度即可

可以用map去重

代码

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

const int N = 200010;

int t, n, k;
int a[N], b[N];

void solve()
{
	cin >> n >> k;
	
	for (int i = 1; i <= n; i ++ ) cin >> a[i];
	
	map<int, int> ump;
	
	int tt = 0;
	
	for (int i = 1; i <= n; i ++ )
	{
		if (!ump[a[i]])
		{
			b[tt ++ ] = a[i];
		}
		ump[a[i]] ++;
	}
	
	sort(b, b + tt);
	
	int res = 0;
	int i, j;
	int temp = 0;
	for (i = 0, j = 0; i < tt; i ++ )
	{
		
		j = max(i, j);
		
		if (j == i)
			temp = ump[b[i]];
		else
			temp -= ump[b[i - 1]];
		
		while (j < tt - 1 && b[j] + 1 == b[j + 1] && j - i + 1 < k)
		{
			j ++;
			temp += ump[b[j]];
		}
		res = max(res, temp);
	}
	
	cout << res << endl;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> t;
	while (t -- )
	{
		solve();
	}
}
相关推荐
学传打活4 天前
【边打字.边学昆仑正义文化】_19_星际生命的生存状况(1)
微信公众平台·1024程序员节·汉字·昆仑正义文化
unable code11 天前
[HNCTF 2022 WEEK2]ez_ssrf
网络安全·web·ctf·1024程序员节
unable code12 天前
[NISACTF 2022]easyssrf
网络安全·web·ctf·1024程序员节
unable code13 天前
BUUCTF-[第二章 web进阶]SSRF Training
网络安全·web·ctf·1024程序员节
开开心心就好14 天前
进程启动瞬间暂停工具,适合调试多开
linux·运维·安全·pdf·智能音箱·智能手表·1024程序员节
仰泳之鹅15 天前
【51单片机】第一课:单片机简介与软件安装
单片机·嵌入式硬件·51单片机·1024程序员节
海海不瞌睡(捏捏王子)15 天前
C#知识点概要
java·开发语言·1024程序员节
小浣熊熊熊熊熊熊熊丶17 天前
飞牛NAS 安装 Teslamate 教程(docker版)
1024程序员节
程高兴17 天前
模糊PID控制的永磁同步电机矢量控制系统-SIMULINK
matlab·1024程序员节
海海不瞌睡(捏捏王子)17 天前
Unity知识点概要
unity·1024程序员节