Uva12174 Shuffle(滑动窗口)

时间:2023-03-08 22:05:53
Uva12174  Shuffle(滑动窗口)

  $play[i]$表示以$i$这个点结束的连续$s$个播放记录是否是无重复的,这样最后只需要枚举可能的播放时间,然后检查对应的播放区间是否是单独的就可以了。特殊情况是,出现的所有播放记录无重复,且长度小于等于$s$,这种情况,答案就是$s$。

AC代码

#include <stdio.h>
#include <string.h>
const int maxn = + ;
int play[maxn], re[maxn], cnt[maxn]; int main() {
int n, s, T;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &s, &n);
memset(play, , sizeof(play));
memset(cnt, , sizeof(cnt));
for(int i = ; i < n; i++) scanf("%d", &re[i]);
int i = , j = ;
int only = ;
while(j < n) {
cnt[re[j]]++;
if(cnt[re[j]] == ) only++;
else only--;
while(only != j-i+) {
cnt[re[i]]--;
if(cnt[re[i]] == ) only++;
else only--;
i++;
}
if(only == s || (j < s && only == j+)) play[j] = ;
j++;
}
j--;
int lim = j - i + ;
int ans = ;
if(lim == n) {
ans = s;
} else {
for(int x = ; x <= lim; x++) {
int y = n - - x;
int flag = ;
while(y >= ) {
if(!play[y]) {
flag = ;
break;
}
y -= s;
}
if(flag) ans++;
}
}
printf("%d\n", ans);
}
return ;
}

如有不当之处欢迎指出!