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——[滑动窗口]的更多相关文章

  1. UVa 12174 Shuffle(滑动窗口)

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

  2. UVa 12174 Shuffle (滑动窗口)

    题意:你正在使用的音乐播放器有一个所谓的乱序播放功能,即随机打乱歌曲的播放顺序.假设一共有s首歌, 则一开始会给这s首歌随机排序,全部播放完毕后再重新随机排序.继续播放,依次类推.注意,当s首歌播放完 ...

  3. Uva12174 Shuffle(滑动窗口)

    $play[i]$表示以$i$这个点结束的连续$s$个播放记录是否是无重复的,这样最后只需要枚举可能的播放时间,然后检查对应的播放区间是否是单独的就可以了.特殊情况是,出现的所有播放记录无重复,且长度 ...

  4. 【uva 12174】Shuffle(算法效率--滑动窗口)

    题意:假设一种音乐播放器有一个乱序的功能,设定每播放S首歌为一个周期,随机播放编号为1~S的歌曲.现在给一个长度为N的部分播放记录,请统计下次随机排序所发生的时间的可能性种数.(1≤S,N≤10000 ...

  5. 紫书 例题8-15 UVa 12174 (滑动窗口)

    这道题就是给你一n长序列, 然后把这个序列按顺序分成很多段, 每段长s(最前面可以小于s, 只有第一段的后半段, 最后面也同样, 只有最后一段的前半段), 然后要求是每一段里面没有重复的数, 问你有几 ...

  6. 数据流滑动窗口平均值 · sliding window average from data stream

    [抄题]: 给出一串整数流和窗口大小,计算滑动窗口中所有整数的平均值. MovingAverage m = new MovingAverage(3); m.next(1) = 1 // 返回 1.00 ...

  7. 滑动窗口的中位数 · Sliding Window Median

    [抄题]: 给定一个包含 n 个整数的数组,和一个大小为 k 的滑动窗口,从左到右在数组中滑动这个窗口,找到数组中每个窗口内的中位数.(如果数组个数是偶数,则在该窗口排序数字后,返回第 N/2 个数字 ...

  8. [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 ...

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

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

随机推荐

  1. spark-ML基础

    一.ML组件 ML的标准API使用管道(pipeline)这样的方式,可以将多个算法或者数据处理过程整合到一个管道或者一个流程里运行,其中包含下面几个部分: 1. dataFrame:用于ML的dat ...

  2. C++:static类

    static自我理解 static使得数据成员或者函数生命周期为整个文件所在程序的生命周期, 在C中还可以用它避免被其它文件使用为外部成员 static类 明确:类的静态数据成员它被所有类对象共享,但 ...

  3. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十八章:立方体贴图

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十八章:立方体贴图 代码工程地址: https://github.c ...

  4. Kubernetes1.4正式发布

    Kubernetes1.4正式发布. 昨天刚预测1.4即将正式发布,结果晚上Kubernetes1.4就正式发布了. 先看看Kubernetes发布历史: Kubernetes 1.0 - 2015年 ...

  5. Validation异常:No validator could be found for constraint '.....' validating type 'java.lang.Integer'.

    javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'java ...

  6. 中文乱码在java中URLEncoder.encode方法要调用两次解决

    中文乱码在java中URLEncoder.encode方法要调用两次解决 一.场景: 1.我在客户端要通过get方式调用服务器端的url,将中文参数做utf-8编码,需要在js中两次的进行编码,服务器 ...

  7. WPF 2048游戏的实现

    原文:WPF 2048游戏的实现         前几天空闲的时候,实现了一个2048游戏.除了可以设置行数和列数之外,支持修改显示名称,比如下面,改成神雕侠侣中的角色名称:           游戏 ...

  8. python 自动登录网页

    语言:python 浏览器:chrome 工具:chrome控制台 #!/usr/bin/python # coding: GBK import urllib,urllib2,httplib,cook ...

  9. window执行python文件

    @echo offD:cd D:\pythonstart python2 del.pyexit

  10. Python中的生成器(generator)

    生成器: 在函数内部包含yield关键字,那么该函数执行的结果就是生成器(生成器即是迭代器) yield的功能:1.把函数的执行结果做成迭代器(帮函数封装好__iter__(),__next__()方 ...