CF1225B2 TV Subscriptions (Hard Version)
题意
给定一个
解法
首先希望审核该题解的管理员给这道题评个难度。
我提供一个本题最差解法,限时
这份题解用的是莫队,因为考虑要求的是区间中出现的不同的数的个数,是莫队经典写法。
但是普通莫队加上部分指令集和快读虽然能过,但是时间很离谱,花了
我们知道莫队有一个地方可以优化,那就是排序。但我指的优化并不是奇偶排序,而是根本不用排序。
明显我们的排序是按
代码:
#pragma GCC optimize("-Ofast")
#pragma GCC target("avx,sse,sse2,sse3,sse4")
#pragma unroll 10
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int N = 1e6 + 5;
int t, n, k, d, cnt[N], a[N], tcnt = 0, len, res = 0;
inline int read()
{
register char ch = getchar();
register int x = 0;
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' and ch <= '9')
{
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x;
}
struct Node
{
int l, r;
bool operator<(const Node& g) const
{
int i = l / len, j = g.l / len;
return (i ^ j ? i < j : i & 1 ? r < g.r : r > g.r);
}
}q[N];
void add(int x)
{
if (++cnt[a[x]] == 1) ++res;
}
void del(int x)
{
if (--cnt[a[x]] == 0) --res;
}
int main()
{
t = read();
for (int i = 1; i <= t; i++)
{
res = 0;
tcnt = 0;
n = read(), k = read(), d = read();
len = sqrt(n);
memset(cnt, 0, sizeof(cnt));
for (int j = 1; j <= n; j++) a[j] = read();
for (int j = 1; j + d - 1 <= n; j++)
{
++tcnt;
q[tcnt].l = j;
q[tcnt].r = j + d - 1;
}
//sort(q + 1, q + tcnt + 1);
int nl(1), nr(0), ans = 1e9;
for (int j = 1; j <= tcnt; j++)
{
int l(q[j].l), r(q[j].r);
while (nl < l) del(nl++);
while (nl > l) add(--nl);
while (nr < r) add(++nr);
while (nr > r) del(nr--);
ans = min(ans, res);
}
printf("%d\n", ans);
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现