题意:

你正在使用的音乐播放器有一个所谓的乱序功能,即随机打乱歌曲的播放顺序。
假设一共有s首歌,则一开始会给这s首歌随机排序,全部播放完毕后再重新随机排序、继续播放,依此类推。
注意,当s首歌播放完毕之前不会重新排序。这样,播放记录里的每s首歌都是1~s的一个排列。
给出一个长度为n(1≤s,n≤100000)的播放记录(不一定是从最开始记录的),你的任务是统计下次随机排序所发生的时间有多少种可能性。
例如,s=4,播放记录是3, 4, 4, 1, 3, 2, 1, 2, 3, 4,不难发现只有一种可能性:前两首是一个段的最后两首歌,后面是两个完整的段,因此答案是1;
当s=3时,播放记录1, 2, 1有两种可能:第一首是一个段,后两首是另一段;前两首是一段,最后一首是另一段。答案为2。

比较难的滑动窗口  还没彻底搞懂  细节多

LRJ

// UVa12174 Shuffle
// Rujia Liu
#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
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
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
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++) {
int valid = ;
for (int j = i; j < n+s+; j += s)
if(!ok[j]) valid = ;;
if(valid) ans++;
}
if(ans == n+) ans = s; // special case
cout << ans << "\n";
}
return ;
}
#include<iostream>
#include<cstring>
#include<set>
using namespace std; const int N = 1e5 + ; int s, n, a[N], vis[N];
bool flag[N];
int ans; void init() {
cin >> s >> n;
int num = ;
for (int i = ; i < n; i++) {
cin >> a[i];
if (i < s) { //对前面的s个进行分析
if (vis[a[i]]) num++; //统计前s个中重复的数字
vis[a[i]]++;
}
} for (int i = ; i < n; i++) {
//如果num=0,说明前s个中没有重复的数字,那么第一个数字可以作为循环的开始
if (num == ) flag[i] = true; //窗口开始滑动
if (vis[a[i]] == ) num--; //如果此时最左边的数为重复了的数,num需要减1
vis[a[i]]--; int k = i + s; //新数字进入滑动窗口
if (k >= n) continue;
if (vis[a[k]]) num++; //如果已经出现过
vis[a[k]]++;
}
} bool judge(int x) {
for (int i = x; i < n; i += s)
if (!flag[i]) return false;
return true;
} void solve() {
memset(vis, , sizeof(vis)); ans = ;
for (int i = ; i < s; i++) {
if (judge(i)) ans++;
if (i >= n) continue;
//从左往右依次遍历,如果当前a[i]前面已经出现过,那么前面必须会有开头,此时必须结束循环
if (vis[a[i]]) break;
vis[a[i]]++;
}
} int main() {
//freopen("D:\\txt.txt", "r", stdin);
int t;
cin >> t;
while (t--) {
memset(flag, , sizeof(flag));
memset(vis, , sizeof(vis));
init();
solve();
cout << ans << endl;
}
return ;
}

8-15 Shuffle uva12174的更多相关文章

  1. 搭建Spark所遇过的坑

    一.经验 1.Spark Streaming包含三种计算模式:nonstate .stateful .window 2.kafka可通过配置文件使用自带的zookeeper集群 3.Spark一切操作 ...

  2. TFlearn——(2)SVHN

    1,数据集简介 SVHN(Street View House Number)Dateset 来源于谷歌街景门牌号码,原生的数据集1也就是官网的 Format 1 是一些原始的未经处理的彩色图片,如下图 ...

  3. spark 笔记 15: ShuffleManager,shuffle map两端的stage/task的桥梁

    无论是Hadoop还是spark,shuffle操作都是决定其性能的重要因素.在不能减少shuffle的情况下,使用一个好的shuffle管理器也是优化性能的重要手段. ShuffleManager的 ...

  4. Uva12174 Shuffle(滑动窗口)

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

  5. [转]完美洗牌(Perfect Shuffle)问题

    [转]原博文地址:https://github.com/julycoding/The-Art-Of-Programming-By-July/blob/master/ebook/zh/02.09.md ...

  6. Apache Spark源码走读之24 -- Sort-based Shuffle的设计与实现

    欢迎转载,转载请注明出处. 概要 Spark 1.1中对spark core的一个重大改进就是引入了sort-based shuffle处理机制,本文就该处理机制的实现进行初步的分析. Sort-ba ...

  7. spark新能优化之shuffle新能调优

    shuffle调优参数 new SparkConf().set("spark.shuffle.consolidateFiles", "true") spark. ...

  8. range,shuffle,str_shuffle

    print_r(range(1,20)); 输出,range产生 Array( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ...

  9. Shuffle和排序

    MapReduce确保每个reducer的输入都按键排序.系统执行排序的过程——将map输出作为输入传给reducer——称为shuffle.shuffle属于不断被优化和改进的代码库的一部分,从许多 ...

随机推荐

  1. Jenkins配置定时任务

    在任务配置中,滚动到构建触发器-->勾选"Build periodically"-->在输入框中配置触发时间 以上配置,表示在6月13日23点触发. 如果配置成  00 ...

  2. 织梦dedecms 模板文件不存在,无法解析文档!

    方法一:[此对应喜欢把模板文件使用".html"的格式,] /include/arc.archives.class.php 556行 if (!preg_match("# ...

  3. MapReduce (hive表SequenceFile的结果做输入)、MultipleOutputs和Reduce端迭代iterable的一些说明

    很长时间以来一直写hive,嵌套脚本.偶尔写UDF.  最近用Hive的dynamic partition和多路插入做一些事情,很遗憾的结果是非常不稳定,有时能成功,有时失败.(可能是因为hive版本 ...

  4. Header File Dependencies

    [Header File Dependencies] 什么时候可以用前置声明替代include? 1.当 declare/define pointer&reference 时. 2.当 dec ...

  5. 【CodeForces】961 G. Partitions 斯特林数

    [题目]G. Partitions [题意]n个数$w_i$,每个非空子集S的价值是$W(S)=|S|\sum_{i\in S}w_i$,一种划分方案的价值是所有非空子集的价值和,求所有划分成k个非空 ...

  6. 截取汉字 mb_sbstr()

    一.中文截取:mb_substr() mb_substr( $str, $start, $length, $encoding ) $str,需要截断的字符串 $start,截断开始处,起始处为0 $l ...

  7. c++程序设计中的函数重载

    函数重载的意思是在一个作用域内(命名空间内)定义了某个或某些具有相同名称的函数,但是他们的参数列表和定义(实现)不相同,如果相同的话,就没啥意义了.当调用一个重载函数时,编译器会通过所使用的参数类型. ...

  8. Centos7网络配置(VMware)

    在VM虚拟机上装了Centos7,想要用xshell5连接操作,配置网络花了整整一个上午的时间,真是心酸. 登陆后,使用命令 ip addr查看了本机的网络 可以看到我的网络配置文件是ens33, 使 ...

  9. aarch64_a1

    AGReader-1.2-16.fc26.aarch64.rpm 2017-02-14 07:01 50K fedora Mirroring Project ATpy-0.9.7-11.fc26.no ...

  10. ASP.net-空白页的问题

    protected void Application_Error(object sender, EventArgs e)         {             ILog log = LogMan ...