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();
	}
}
相关推荐
开开心心就好5 天前
开源免费高速看图工具,支持漫画大图秒开
linux·运维·服务器·安全·ruby·symfony·1024程序员节
unable code8 天前
磁盘取证-Flying_High
网络安全·ctf·misc·1024程序员节·磁盘取证
unable code9 天前
磁盘取证-ColorfulDisk
网络安全·ctf·misc·1024程序员节·内存取证
unable code9 天前
磁盘取证-[第十章][10.1.2 磁盘取证方法]磁盘取证1
网络安全·ctf·misc·1024程序员节·内存取证
开开心心就好11 天前
免费抽奖工具支持批量导入+自定义主题
linux·运维·服务器·macos·pdf·phpstorm·1024程序员节
开开心心就好15 天前
卸载工具清理残留,检测垃圾颜色标识状态
linux·运维·服务器·python·安全·tornado·1024程序员节
子燕若水16 天前
Facebook reels 运营指南
1024程序员节
尘觉19 天前
创作 1024 天|把热爱写成长期主义
数据库·1024程序员节
写点什么呢20 天前
Word使用记录
word·1024程序员节
开开心心就好20 天前
内存清理工具点击清理,自动间隔自启
linux·运维·服务器·安全·硬件架构·材料工程·1024程序员节