uva12174 滑动窗口+预处理
注意理解题意,不是排列种类,而是下一个排序出现的时间
滑动窗口,具体见代码,写了很多注释(紫书的思路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 滑动窗口+预处理的更多相关文章
- UVa 12174 Shuffle(滑动窗口)
https://vjudge.net/problem/UVA-12174 题意: 你在听音乐播放器,它采用随机播放形式.随机播放的原理时先随机产生一个1~n的排列,然后就按这个排列顺序播放歌曲.播放完 ...
- bzoj 2093 [Poi2010]Frog——滑动窗口
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2093 找第k近的可以用一个含k个元素的滑动窗口来实现. 卡空间也还行,但卡时间.不要预处理倍 ...
- 单调队列优化&&P1886 滑动窗口题解
单调队列: 顾名思义,就是队列中元素是单调的(单增或者单减). 在某些问题中能够优化复杂度. 在dp问题中,有一个专题动态规划的单调队列优化,以后会更新(现在还是太菜了不会). 在你看到类似于滑动定长 ...
- 12174 - Shuffle——[滑动窗口]
You are listening to your music collection using the shuffle function to keep the music surprising. ...
- LeetCode#3 - 无重复字符的最长字串(滑动窗口)
题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例: abcabcbb 输出的结果应该是3,最长的无重复的字串是'abc' 果然无论做什么都要静下心来啊!昨晚上卡了一个多小 ...
- [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 ...
- TCP/IP 协议中的滑动窗口
一个例子明白发送缓冲区.接受缓冲区.滑动窗口协议之间的关系. 在上面的几篇文章中简单介绍了上述几个概念在TCP网络编程中的关系,也对应了几个基本socket系统调用的几个行为,这里再列举一个例子,由于 ...
- Storm Windowing storm滑动窗口简介
Storm Windowing 简介 Storm可同时处理窗口内的所有tuple.窗口可以从时间或数量上来划分,由如下两个因素决定: 窗口的长度,可以是时间间隔或Tuple数量: 滑动间隔(slidi ...
- lintcode 滑动窗口的最大值(双端队列)
题目链接:http://www.lintcode.com/zh-cn/problem/sliding-window-maximum/# 滑动窗口的最大值 给出一个可能包含重复的整数数组,和一个大小为 ...
随机推荐
- 在linux下用tomcat部署java web项目的过程与注意事项(转)
在linux下用tomcat部署java web项目的过程与注意事项一.安装JDK到http://www.oracle.com/technetwork/java/javase/downloads/in ...
- 【旧文章搬运】Windbg+Vmware驱动调试入门(四)---VirtualKD内核调试加速工具
原文发表于百度空间,2009-01-09========================================================================== 今天又想起 ...
- VMware Workstation安装centos 6.5详细步骤
转自“http://blog.51cto.com/12496630/2058386” 22.选择分区了,centos新版中使用lvm来分区,我不用过分去计算分区大小,这个模式可以允许用户以后动态调整分 ...
- linux 下 读取某个文件的某一行或者某几行
wc -l a.txt 统计a.txt 行数 查看文件a.txt的第190行到196行, sed -n '190,196p' a.txt 如果查看某一行用 sed -n 'a,bp' a.txt ...
- from表单POST提交nodejs
var form = new formidable.IncomingForm(); form.parse(req, function(err, fields, files) { console.log ...
- 玩下GourdScan
GourdScan是一个分布式SQL漏洞扫描器,采用Python+PHP开发,后端采用SQLMAP.通过浏览器代理方式获取请求进行漏洞检测. win10 准备: phpstudy python2.7 ...
- 洛谷 - P1739 - 表达式括号匹配 - 模拟 - 栈
https://www.luogu.org/problemnew/show/P1739 虽然应该是用栈的……但是直接模拟就可以了. #include<bits/stdc++.h> usin ...
- bzoj 3261 最大异或和【可持久化trie】
因为在后面加数字又求后缀和太麻烦,所以xor[p...n]=xor[1...n]^xor[p-1...n]. 首先处理出来区间异或前缀和,对前缀和建trie树(在最前面放一棵0表示最开始的前缀和 然后 ...
- 洛谷P2569 [SCOI2010]股票交易(单调队列)
传送门 惭愧……这种题目都没看出来…… 首先,我们用$dp[i][j]$表示在第$i$天,手上有$j$股时的最大收益 第一,我们可以直接买股票,即$dp[i][j]=-j*AP_i$,这个直接计算即可 ...
- the little schemer 笔记(4)
第四章 numbers games 14 是原子吗 是的,数都是原子 (atom? n) 是真还是假,其中n是14 真,14 是原子 -3是数吗 是的,不过我们暂不考虑负数 3.14159是数吗 是的 ...