注意理解题意,不是排列种类,而是下一个排序出现的时间
滑动窗口,具体见代码,写了很多注释(紫书的思路1理解有点麻烦...)
注:可以画一个轴来方便理解

#include<iostream>
#include<vector>
using namespace std; const int maxn = + ;
int s, n, x[maxn*], cnt[maxn], ok[maxn*]; int main() {
int T;
cin >> T;
while(T--) {
cin >> s >> n;
// add s "-1" to the left/right of orriginal sequence
// so we don't have to worry about negative subscript or wrapping round
fill(x, x+n+*s, -);
for(int i = ; i < n; i++) cin >> x[i+s]; int tot = ; // how many different integers in current sliding window //112有两个1,2
fill(cnt+, cnt+s+, ); // cnt[i] is the number of occurrence of i in the current sliding window
fill(ok, ok+n+s+, ); // ok[i] = 1 iff the i-th sliding window didn't have duplicate numbers // compute "ok" array //预处理
//每个窗口可以看做是n中某个数为左端点或者右端点的连续s个数
//tot统计的是窗口中数字出现的次数,等同于属于n个数的数字出现的次数;所以赋值为-1的不用管
//分成窗口自己作为完整s,和左边的不确定的数组合成为s(此时窗口没有完全进入n),和右边三种情况
for(int i = ; i < n+s+; i++) {
if (tot == s) ok[i] = ; // complete window 这个窗口中每个数字恰好出现一次
if (i < s && tot == i) ok[i] = ; // incomplete windows on the left side 这个i也是n个数中纳入窗口的数量,tot=i那么加上左边的-1就可以组成s 看做右端点
if (i > n && tot == n+s-i) ok[i] = ; // incomplete windows on the right side 看做左端点 // update cnt and tot for the next sliding window //注意是在计算下一个窗口
if (i == n+s) break; // no more sliding windows, so we stop here
if (x[i] != - && --cnt[x[i]]==) tot--; // remove the first one 当前窗口第一个数减掉
if (x[i+s] != - && cnt[x[i+s]]++==) tot++; // add the next one 下一个数加入了下一个窗口
} // check each possible answer
int ans = ;
for(int i = ; i < s; i++) { //n中前s个数的各种情况(想一想就知道最后新的开始只和前面数量不大于s的数相关)
int valid = ;
for (int j = i; j < n+s+; j += s)
if(!ok[j]) valid = ;//i相当于取n中的前i个数做s,之后每次加s相当于再一个窗口;需要每个窗口都满足条件才可以是一种可行方案
if(valid) ans++;
}
if(ans == n+) ans = s; // special case 此时说明n个数可以作为整体作为开头或结尾或中间,就有ans=n-1(不作为整体)+s-n+1(作为整体)
cout << ans << "\n";
}
return ;
}

uva12174 滑动窗口+预处理的更多相关文章

  1. UVa 12174 Shuffle(滑动窗口)

    https://vjudge.net/problem/UVA-12174 题意: 你在听音乐播放器,它采用随机播放形式.随机播放的原理时先随机产生一个1~n的排列,然后就按这个排列顺序播放歌曲.播放完 ...

  2. bzoj 2093 [Poi2010]Frog——滑动窗口

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2093 找第k近的可以用一个含k个元素的滑动窗口来实现. 卡空间也还行,但卡时间.不要预处理倍 ...

  3. 单调队列优化&&P1886 滑动窗口题解

    单调队列: 顾名思义,就是队列中元素是单调的(单增或者单减). 在某些问题中能够优化复杂度. 在dp问题中,有一个专题动态规划的单调队列优化,以后会更新(现在还是太菜了不会). 在你看到类似于滑动定长 ...

  4. 12174 - Shuffle——[滑动窗口]

    You are listening to your music collection using the shuffle function to keep the music surprising. ...

  5. LeetCode#3 - 无重复字符的最长字串(滑动窗口)

    题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例: abcabcbb 输出的结果应该是3,最长的无重复的字串是'abc' 果然无论做什么都要静下心来啊!昨晚上卡了一个多小 ...

  6. [LeetCode] Sliding Window Maximum 滑动窗口最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  7. TCP/IP 协议中的滑动窗口

    一个例子明白发送缓冲区.接受缓冲区.滑动窗口协议之间的关系. 在上面的几篇文章中简单介绍了上述几个概念在TCP网络编程中的关系,也对应了几个基本socket系统调用的几个行为,这里再列举一个例子,由于 ...

  8. Storm Windowing storm滑动窗口简介

    Storm Windowing 简介 Storm可同时处理窗口内的所有tuple.窗口可以从时间或数量上来划分,由如下两个因素决定: 窗口的长度,可以是时间间隔或Tuple数量: 滑动间隔(slidi ...

  9. lintcode 滑动窗口的最大值(双端队列)

    题目链接:http://www.lintcode.com/zh-cn/problem/sliding-window-maximum/# 滑动窗口的最大值 给出一个可能包含重复的整数数组,和一个大小为  ...

随机推荐

  1. python中为什么需要使用“if __name__ == '__main__'”语句

    首先用最简洁的语言来说明一下 if __name__ == '__main__': 的作用:防止在被其他文件导入时显示多余的程序主体部分. 先举个例子,如果不用 if __name__ == '__m ...

  2. 【旧文章搬运】炉子给的SYSTEM_HANDLE_TYPE有点错误

    原文发表于百度空间,2008-12-03========================================================================== 今天写程序 ...

  3. Bootstrap Notify

    https://github.com/mouse0270/bootstrap-notify $.notify('Hello World', { offset: { x: 50, y: 100 } }) ...

  4. hdoj1150(最小点覆盖)

    题意: 两台机器,A台机器有N种模式,B台机器有M种不同的模式,初始模式都是0 以及K个需要运行的任务(i,x,y),在A台机器是x模式,在B台机器是y模式. 请合理为每个任务安排一台机器并合理安排顺 ...

  5. go语言 rsa加密

    // rsa.go package main import ( "crypto/rand" "crypto/rsa" "crypto/x509&quo ...

  6. 公司内网,无法使用yum在线下载,肿么办?

    1 配置上网代理 编辑/etc/yum.conf,增加如下属性: proxy=你的代理地址 proxy_user=你的用户名 proxy_password=你的密码 2 配置国内163 yum源 备份 ...

  7. Jquery Validate不是用submit按钮提交表单,使用a标签js代码都可以

    不多说,上代码. $("#form").validate(); $("#btn").click(function() { if($("#form&qu ...

  8. 黑客攻防技术宝典web实战篇:攻击用户·其他技巧习题

    猫宁!!! 参考链接:http://www.ituring.com.cn/book/885 随书答案. 1. 已知一项应用程序功能将一个查询字符串参数的内容插入到某个 HTTP 重定向的 Locati ...

  9. Ubuntu 18.04 LTS 安装后 各种问题以及解决方案

    1. root的初始密码,默认是不知道的,需要进行设置 a. 进入终端自己的用户 b. 输入 sudo passwd回车 c. 输入新密码,回车,重复,回车,搞定 d. su 一下,就可以了 2.  ...

  10. sql server 获取数据字段(表的字段和类型信息)

    获取数据字段(表的字段和类型信息) SELECT 表名= then d.name else '' end, 表说明= then isnull(f.value,'') else '' end, 字段序号 ...