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/# 滑动窗口的最大值 给出一个可能包含重复的整数数组,和一个大小为 ...
随机推荐
- CodeBlocks+Qt(MinGW)配置
1.安装CodeBlocks 官网:http://www.codeblocks.org/ 下载地址:http://www.codeblocks.org/downloads/26 有以下两种选择 cod ...
- String、StringBuffer和StringBuilder有什么区别?
1. String 类 String的值是不可变的,这就导致每次对String的操作都会生成新的String对象,不仅效率低下,而且大量浪费有限的内存空间.String a = "a&quo ...
- Centos系统真机安装,U盘方式
下载Centos系统镜像,建议选择Minimal ISO.下载地址:https://www.centos.org/download/ 下载Fedora Media Writer,用来将系统镜像写到U盘 ...
- k8s-调度器、预选策略及优选函数-二十
一.简介 master上运行着三个最核心的组件,apiserver.scheduler.controller manager.此外,master还依赖于ectd存储节点,最好ectd是有冗余能力的集群 ...
- gitbucket
github固然强大,但不怎么适合企业级的开发版本管理,原因相信大家都明白,首先在github上上传和拉取代码速度是比较慢的,再者,在Github上创建一个私有仓库是收费的,那么gitbucket是一 ...
- 一个NodeJS写的基于MVC的服务器
目前实现了静态文件下载.根据地址导航到控制器相应的控制器方法,但视图模版功能目前还未实现. 服务器代码(httpserver.js): var http = require("http&qu ...
- 洛谷 - P2335 - 位图 - 简单dp
https://www.luogu.org/problemnew/show/P2335 假如我们使用dp的话,每次求出一个点的左上方.右上方.左下方.右下方的最近的白点的距离.那么只是n²的复杂度.这 ...
- 瞎比比系列---1st
A - 项目管理HDU4858 /* 题意: 这个项目有n个节点, 两个节点间可能有多条边,不过一条边的两端必然是不同的节点. 0的时候:接下来两个数u v表示给项目u的能量值加上v: 1的时候: 这 ...
- 如何让Android微博个人详情页滚动到顶部
版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/103 个人详情页滑动到顶部 最近产品提了个新需求,需要实现 ...
- 51Nod 1649 齐头并进
#include <iostream> #include <algorithm> #include <cstring> //两遍迪杰斯特拉 #define INF ...