P12008 【MX-X10-T4】[LSOT-4] Fragment of Memories 题解
一. 题面:点这里
二. 思路:
首先需要感谢 良心WA题人 巨佬提供的复杂度证明思路,好人一生平安。
首先考虑答案的性质,发现答案具备单调性,所以考虑二分答案
考虑优化,显然枚举每一个记忆起始数字这个部分是每一次的答案贡献,我们很难优化,那么考虑 check 的部分,思考具体的过程。每一次 check 我们都是扫一遍,遇到最近的
到目前为止,我们已经可以拿到 82pts 了,但是还可以更加优化。注意到一个性质,若起始值为
在最终实现上还有对代码运行速度的优化,所以建议读者阅读本人的代码以作参考。
三. Code:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
inline int read() {
int x=0,f=1;char ch=getchar();
while(ch > '9' || ch < '0'){if(ch == '-'){f = -1;}ch = getchar();}
while(ch >= '0'&&ch <= '9'){x = x * 10 + ch - 48; ch = getchar();}
return x * f;
}
void write(int x) {
if (x > 9) {
write(x / 10);
}
putchar(x % 10 + 48);
}
const int N = 2e6 + 10,LOGN = 24;
std::vector<int> pos[N];
int n,k,V,f[N][LOGN],tmp[N],a[N],Log[N];
int retn_pos(int id,int v)
{
if(id > n && v > V) return n + 1;
return *std::upper_bound(pos[v].begin(),pos[v].end(),id);
}
int lowbit(int x){return x & (-x);}
bool check(int num,int m)
{
--m;
int ps = 0;
int tp[21];
ps = retn_pos(ps,num);
int cnt = 0,j;
while(m)
{
j = lowbit(m);
m -= j;
j = Log[j];
ps = f[ps][j];
tp[++cnt] = j;
if(ps == n + 1) return false;
}
if(ps == n + 1) return false;
for(int i = 1;i <= k - 1;++i)
{
ps = retn_pos(ps,num);
for(int j = 1;j <= cnt;++j)
{
ps = f[ps][tp[j]];
if(ps == n + 1) return false;
}
if(ps == n + 1) return false;
}
return true;
}
int main()
{
n = read(),k = read(),V = read();
for(int i = 2;i <= n;++i) Log[i] = Log[i >> 1] + 1;
for(int i = 1;i <= n;++i)
{
a[i] = read();
pos[a[i]].push_back(i);
}
for(int i = 1;i <= V;++i) pos[i].push_back(n + 1);
for(int i = n;i >= 1;--i)
{
if(!tmp[a[i] + 1]) f[i][0] = n + 1;
else f[i][0] = tmp[a[i] + 1];
tmp[a[i]] = i;
}
f[n + 1][0] = n + 1;
for(int j = 1;j <= 20;++j)
for(int i = 1;i <= n + 1;++i)
f[i][j] = f[f[i][j - 1]][j - 1];
int ans = 0;
for(int i = 1;i <= V;++i)
{
ans = std::max(0,ans - 1);
while(check(i,ans + 1)) ++ans;
write(ans);
putchar(32);
}
return 0;
}