12174 - Shuffle——[滑动窗口]
You are listening to your music collection using the shuffle function to keep the music surprising. You
assume that the shuffle algorithm of your music player makes a random permutation of the songs in
the playlist and plays the songs in that order until all songs have been played. Then it reshuffles and
starts playing the list again.
You have a history of the songs that have been played. However, your record of the history of played
songs is not complete, as you started recording songs at a certain point in time and a number of songs
might already have been played. From this history, you want to know at how many different points in
the future the next reshuffle might occur.
A potential future reshuffle position is valid if it divides the recorded history into intervals of length
s (the number of songs in the playlist) with the rst and last interval possibly containing less than s
songs and no interval contains a specic song more than once.
Input
On the rst line one positive number: the number of testcases, at most 100. After that per testcase:
• One line with two integers s and n (1 ≤ s, n ≤ 100000): the number of different songs in the
playlist and the number of songs in the recorded playlist history.
• One line with n space separated integers, x1, x2, . . . , xn (1 ≤ xi ≤ s): the recorded playlist history.
Output
Per testcase:
• One line with the number of future positions the next reshuffle can be at. If the history could
not be generated by the above mentioned algorithm, output 0.
Sample Input
4
4 10
3 4 4 1 3 2 1 2 3 4
6 6
6 5 4 3 2 1
3 5
3 3 1 1 1
7 3
5 7 3
Sample Output
1
6
0
7
题意理解:
给定长度为n的播放历史记录,推断下一次重排的时间点的可能情况数目。注意首尾两段的记录允许不完整(小于s)。
特殊情况:当n<s时,如果记录中的每首歌均无重复,比如:
7 3
5 7 3
5号歌之前可能还存在1~7(大于7时的情况是等效的)首歌已经播放却没有加入记录,那么下一次重排点就有这7种情况。
解题思路:
使用滑动窗口的思想进行预处理:对n中的第i个数,如果紧随其后的s个数均没有重复,那么以i为起始的窗口是合法的。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <set>
#define time_ printf("time = %f\n",double(clock())/CLOCKS_PER_SEC)
using namespace std;
const int maxn=;
int s,n;
int seq[maxn+];
int num[maxn+];
int id[maxn+]; void pre_process(){
int l=,r=;
memset(num,,sizeof num);
memset(id,,sizeof id);
int single=;
for(;r<l+s&&r<n;r++){
int id=seq[r];
num[id]++;
if(num[id]==) single++;
else single--;
}
if(single==r-l) id[l]=;
while(l<n){
int a=seq[l];
int b=seq[r];
num[a]--;
if(num[a]==) single--;
if(num[a]==) single++;
l++;
if(r!=n){
num[b]++;
if(num[b]==) single++;
if(num[b]==) single--;
r++;
}
if(l!=n&&single==r-l) id[l]=;
}
}
int main(int argc, const char * argv[]) {
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&s,&n);
for(int i=;i<n;i++)
scanf("%d",&seq[i]);
pre_process();
int ans=;
int rst[maxn+];
memset(rst,,sizeof rst);
rst[seq[]]++;
int single=;
for(int d=;d<=s&&d<=n;d++){
bool ok=true;
if(single!=d) break;;
for(int pt=d;pt<n;pt+=s){
if(id[pt]==){
ok=false;
break;
}
}
if(ok) ans++;
rst[seq[d]]++;
if(rst[seq[d]]==) single++;
}
if(n<s&&ans==n)
ans=s;
printf("%d\n",ans);
}
return ;
}
12174 - Shuffle——[滑动窗口]的更多相关文章
- UVa 12174 Shuffle(滑动窗口)
https://vjudge.net/problem/UVA-12174 题意: 你在听音乐播放器,它采用随机播放形式.随机播放的原理时先随机产生一个1~n的排列,然后就按这个排列顺序播放歌曲.播放完 ...
- UVa 12174 Shuffle (滑动窗口)
题意:你正在使用的音乐播放器有一个所谓的乱序播放功能,即随机打乱歌曲的播放顺序.假设一共有s首歌, 则一开始会给这s首歌随机排序,全部播放完毕后再重新随机排序.继续播放,依次类推.注意,当s首歌播放完 ...
- Uva12174 Shuffle(滑动窗口)
$play[i]$表示以$i$这个点结束的连续$s$个播放记录是否是无重复的,这样最后只需要枚举可能的播放时间,然后检查对应的播放区间是否是单独的就可以了.特殊情况是,出现的所有播放记录无重复,且长度 ...
- 【uva 12174】Shuffle(算法效率--滑动窗口)
题意:假设一种音乐播放器有一个乱序的功能,设定每播放S首歌为一个周期,随机播放编号为1~S的歌曲.现在给一个长度为N的部分播放记录,请统计下次随机排序所发生的时间的可能性种数.(1≤S,N≤10000 ...
- 紫书 例题8-15 UVa 12174 (滑动窗口)
这道题就是给你一n长序列, 然后把这个序列按顺序分成很多段, 每段长s(最前面可以小于s, 只有第一段的后半段, 最后面也同样, 只有最后一段的前半段), 然后要求是每一段里面没有重复的数, 问你有几 ...
- 数据流滑动窗口平均值 · sliding window average from data stream
[抄题]: 给出一串整数流和窗口大小,计算滑动窗口中所有整数的平均值. MovingAverage m = new MovingAverage(3); m.next(1) = 1 // 返回 1.00 ...
- 滑动窗口的中位数 · Sliding Window Median
[抄题]: 给定一个包含 n 个整数的数组,和一个大小为 k 的滑动窗口,从左到右在数组中滑动这个窗口,找到数组中每个窗口内的中位数.(如果数组个数是偶数,则在该窗口排序数字后,返回第 N/2 个数字 ...
- [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系统调用的几个行为,这里再列举一个例子,由于 ...
随机推荐
- Leetcode697.Degree of an Array数组的度
给定一个非空且只包含非负数的整数数组 nums, 数组的度的定义是指数组里任一元素出现频数的最大值. 你的任务是找到与 nums 拥有相同大小的度的最短连续子数组,返回其长度. 示例 1: 输入: [ ...
- spss命令数据整理中compute与record命令的区别
spss命令数据整理中compute与record命令的区别 record修改存在的变量,或者生成新的变量 spss变量定义说明 1.Name:变量名,定义规则与其它软件中的雷同,如第一个字符必须为字 ...
- 【JZOJ4784】【NOIP2016提高A组模拟9.15】Map
题目描述 输入 输出 样例输入 4 4 2 1 2 2 3 3 2 3 4 1 2 1 4 样例输出 14 数据范围 样例解释 upd:保证原图连通. "不相交路径"的定义为不存在 ...
- JAVA高级特性--自动拆箱-装箱,枚举类型
基本数据类型转换为引用类型对象 一个自动装箱的例子 Integer i=10; 相当于 Integer i=new Integer(10); 一个自动拆箱的例子 Integer m=10; int n ...
- python 自主控制异常:用户自定义异常
- LeetCode112 Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- ELK练习
1.ELK练习 PUT s3/_doc/ { "mappings" : { "doc" : { "properties" : { " ...
- 日志 5.27 关于AssetBundle
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zxsean/article/details/27228783 大概日志就这么写的吧.没什么太专业的东 ...
- JVM 调优 —— GC 长时间停顿问题及解决方法
零. 简介 垃圾收集器长时间停顿,表现在 Web 页面上可能是页面响应码 500 之类的服务器错误问题,如果是个支付过程可能会导致支付失败,将造成公司的直接经济损失,程序员要尽量避免或者说减少此类情况 ...
- oracle函数 TRIM(c1 from c2)
[功能]删除左边和右边出现的字符串 [参数]C2 删除前字符串 c1 删除字符串,默认为空格 [返回]字符型 [示例] select TRIM('X' from 'XXXgao qian jingXX ...