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系统调用的几个行为,这里再列举一个例子,由于 ...
随机推荐
- list reverse
You can make use of the reversed function for this as: >>> array=[0,10,20,40] >>> ...
- 50倍时空算力提升,阿里云RDS PostgreSQL GPU版本上线
2019年3月19日,阿里云RDS PostgreSQL数据库GPU规格版本正式上线,开启了RDS异构计算并行加速之路.该版本在RDS(关系型数据库服务)的云基础设施层面首次完成了与阿里云异构计算产品 ...
- 单行中文字和图片的相关height和line-height特性
这几天在做仿京东的产品页,发现在制作过程中的一些问题,需要好好研究下. 需要实现的效果如上图所示: 在写CSS样式的时候,对于我的关于竖线的做法是: 设置高度为14,border样式,但导致了一个问题 ...
- Spring_自动组件扫描和 基于注解配置bean
自动组件扫描 启用Spring组件扫描功能. 使用@Component注释来表示这是类是一个自动扫描组件. package com.tanlei.dao; import org.springfram ...
- 10分钟学会Python
#1. 语法 Python中没有强制的语句终止字符,代码块是通过缩进来指示的.缩进表示一个代码块的开始,逆缩进则表示一个代码块的结束.一般用4个空格来表示缩进. 声明以冒号(:)字符结束,并且开启一个 ...
- NOIP2016提高A组模拟9.28总结
这次三道题都是可以AC的. 每道题思路都正确,但每道题都有细节没有注意. 第一题 1.没注意系数为1时可以省略系数: 2.没注意在第一项处理常数后,不能输出+号. 导致丢失20分:一定要多出特殊数据, ...
- oracle如何启用审计
通过数据库初始化参数文件中的AUDIT_TRAIL 初始化参数启用和禁用数据库审计. DB 启用数据库审计并引导所有审计记录到数据库的审计跟踪 OS 启用数据库审计并引导所有审计记录到操作系统的审 ...
- 重磅!容器集群监控利器 阿里云Prometheus 正式免费公测
Prometheus 作为容器生态下集群监控的首选方案,是一套开源的系统监控报警框架.它启发于 Google 的 borgmon 监控系统,并于 2015 年正式发布.2016 年,Prometheu ...
- 【时光回溯】【JZOJ3568】【GDKOI2014】小纪的作业题
题目描述 输入 输出 有M行,每个询问一行,输出结果mod 1,000,000,007的值. 样例输入 10 3 3 5 1 2 3 1 3 5 2 1 7 9 3 9 2 3 样例输出 10 19 ...
- s3c6410时钟初始化
今天自己写bootloader做时钟初始化时遇到的问题,特记录下来.为了方便理解,我大部分都有截图, 在此我先说明下,图均来自数据手冊.也希望看了本篇文章的同志多多參看数据手冊才干理解的更加透 ...