CF1225B2 TV Subscriptions (Hard Version)
题意
给定一个 个元素的序列 ,另外还给定 ,其中 ,现在要求的是所有 区间中出现的不同的数的个数最小是多少,其中 。
解法
首先希望审核该题解的管理员给这道题评个难度。
我提供一个本题最差解法,限时 ,这份题解最坏的数据点跑了 ,还开了部分的指令集,不过用这份代码交到本题的弱化版上却跑得很快,最坏的点只跑了 ,在洛谷轻松 Rank1。
这份题解用的是莫队,因为考虑要求的是区间中出现的不同的数的个数,是莫队经典写法。
但是普通莫队加上部分指令集和快读虽然能过,但是时间很离谱,花了 ,并且只能用 GNU C++14 才能过,GNU C++17 (64) 过不去。
我们知道莫队有一个地方可以优化,那就是排序。但我指的优化并不是奇偶排序,而是根本不用排序。
明显我们的排序是按 分块, 递增的排序。但是这道题中显然 一开始都是递增的,根本不用排序,这样即可过此题。
代码:
#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;
}

浙公网安备 33010602011771号