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();
	}
}
相关推荐
yBmZlQzJ2 天前
财运到内网穿透域名解析技术机制与中立评估
运维·经验分享·docker·容器·1024程序员节
yBmZlQzJ2 天前
内网穿透工具通过端口转发实现内外网通信
运维·经验分享·docker·容器·1024程序员节
数据皮皮侠AI2 天前
数字经济政策工具变量数据(2008-2023)
大数据·数据库·人工智能·笔记·1024程序员节
网安_秋刀鱼3 天前
【java安全】shiro反序列化1(shiro550)
java·开发语言·安全·web安全·网络安全·1024程序员节
unable code5 天前
攻防世界-Misc-Wire1
网络安全·ctf·misc·1024程序员节
开开心心就好6 天前
版本转换工具,支持Win双系统零售批量版
linux·运维·服务器·pdf·散列表·零售·1024程序员节
开开心心就好6 天前
免费卸载工具,可清理残留批量管理启动项
linux·运维·服务器·windows·随机森林·pdf·1024程序员节
unable code6 天前
攻防世界-Misc-4-1
网络安全·ctf·misc·1024程序员节
yBmZlQzJ8 天前
免费内网穿透-端口转发配置介绍
运维·经验分享·docker·容器·1024程序员节