Shuffle UVA - 12174

考察滑动窗口机制的一道题目,为了操作的统一,对于输入的s以及n,我们假设数据的长度是s+n+s,中间的n为真正的数据内容,同时用一个数组amount记录每个数字的出现次数,然后将窗口的位置从最左边开始,刚开始统计的窗口的实质长度是小于s的,然后中间会有一段正好对于s,最后处理的那一个部分还是小于s的,同时每次加入新的数字时更新计数,如果满足对应的计数条件,才能更新对应的长度计数,同时在最后要对得出的特殊结果进行特殊处理,具体实现见如下代码:

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
#include<functional>
using namespace std;

int s, n;
const int maxn = 100000 + 5;
int amount[maxn], exist[maxn * 2],data[maxn*3];

int main(){
	int Case;
	cin >> Case;
	while (Case--){
		cin >> s >> n;
		fill(data, data + 2*s + n, -1);
		fill(exist, exist + s + n+1, 0);
		fill(amount + 1, amount + s + 1, 0);
		int total = 0;
		for (int i = 0; i < n; i++)
			cin >> data[s + i];
		for (int i = 0; i < s + n+1; i++){
			if (total == s) exist[i] = 1;
			if (i < s&&total == i) exist[i] = 1;
			if (i > n&&total == s + n - i) exist[i] = 1;
			
			if (i == s + n) break;

			if (data[i] != -1 && --amount[data[i]] == 0){
				total--;
			}

			if (data[i + s] != -1 && amount[data[i + s]]++ == 0){
				total++;
			}
		}

		int ans = 0;
		
		for (int i = 0; i < s; i++){
			int use = 1;
			for (int j = i; j < s + n+1; j += s){
				if (!exist[j]){
					use = 0;
					break;
				}
			}
			if (use) ans++;
		}
		if (ans == n + 1) ans=s;
		cout << ans << endl;
	}
	return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值