8-15 Shuffle uva12174
题意:
你正在使用的音乐播放器有一个所谓的乱序功能,即随机打乱歌曲的播放顺序。
假设一共有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的更多相关文章
- 搭建Spark所遇过的坑
一.经验 1.Spark Streaming包含三种计算模式:nonstate .stateful .window 2.kafka可通过配置文件使用自带的zookeeper集群 3.Spark一切操作 ...
- TFlearn——(2)SVHN
1,数据集简介 SVHN(Street View House Number)Dateset 来源于谷歌街景门牌号码,原生的数据集1也就是官网的 Format 1 是一些原始的未经处理的彩色图片,如下图 ...
- spark 笔记 15: ShuffleManager,shuffle map两端的stage/task的桥梁
无论是Hadoop还是spark,shuffle操作都是决定其性能的重要因素.在不能减少shuffle的情况下,使用一个好的shuffle管理器也是优化性能的重要手段. ShuffleManager的 ...
- Uva12174 Shuffle(滑动窗口)
$play[i]$表示以$i$这个点结束的连续$s$个播放记录是否是无重复的,这样最后只需要枚举可能的播放时间,然后检查对应的播放区间是否是单独的就可以了.特殊情况是,出现的所有播放记录无重复,且长度 ...
- [转]完美洗牌(Perfect Shuffle)问题
[转]原博文地址:https://github.com/julycoding/The-Art-Of-Programming-By-July/blob/master/ebook/zh/02.09.md ...
- Apache Spark源码走读之24 -- Sort-based Shuffle的设计与实现
欢迎转载,转载请注明出处. 概要 Spark 1.1中对spark core的一个重大改进就是引入了sort-based shuffle处理机制,本文就该处理机制的实现进行初步的分析. Sort-ba ...
- spark新能优化之shuffle新能调优
shuffle调优参数 new SparkConf().set("spark.shuffle.consolidateFiles", "true") spark. ...
- range,shuffle,str_shuffle
print_r(range(1,20)); 输出,range产生 Array( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ...
- Shuffle和排序
MapReduce确保每个reducer的输入都按键排序.系统执行排序的过程——将map输出作为输入传给reducer——称为shuffle.shuffle属于不断被优化和改进的代码库的一部分,从许多 ...
随机推荐
- noi题库(noi.openjudge.cn) 3.9数据结构之C++STL T1——T2
T1 1806:词典 描述 你旅游到了一个国外的城市.那里的人们说的外国语言你不能理解.不过幸运的是,你有一本词典可以帮助你. 输入首先输入一个词典,词典中包含不超过100000个词条,每个词条占据一 ...
- sql 存储时空格转成问号问题
最近做系统,从邮件中导出邮件,上传到系统中,遇到一个奇葩的问题,如下: 通过本地文件看,文件名中是一个空格,上传至数据库后,展示就变成了问号,究其原因,发现是一个特殊字符导致: 最近认真去查了一下这个 ...
- Centos7系统中安装Nginx1.8.0
Nginx的安装 tar -zxvf nginx-1.8.0.tar.gz cd nginx-1.8.0 ./configure make make install /usr/local/nginx/ ...
- Flash数据的采集方法-搜房房价走势采集
一般来说flash中的数据是不能被现有技术很容易采集到的,但是也不能谈flash色变,要具体问题具体分析,有些flash是可以通过一些分析发现背后的数据.然后采集就变得很容易了. 具体案例:搜房房价走 ...
- 最小主义:我的Musca桌面环境
我现在有一个非常简单实用的桌面环境了:Musca + conky + trayer. 当然Musca运行时需要dmenu,其实也不是非dmenu不可,据说 dzen 也不错. 我现在用的是dmenu. ...
- java 根据二叉树前序 ,中序求后续
在一棵二叉树总,前序遍历结果为:ABDGCEFH,中序遍历结果为:DGBAECHF,求后序遍历结果. 我们知道: 前序遍历方式为:根节点->左子树->右子树 中序遍历方式为:左子树-> ...
- django的缓存,信号,序列化
一 Django的缓存机制 1.1 缓存介绍 1.缓存的简介 在动态网站中,用户所有的请求,服务器都会去数据库中进行相应的增,删,查,改,渲染模板,执行业务逻辑,最后生成用户看到的页面. 当一个网站的 ...
- 配置多个ssh-key
搞了三天没搞出来,还在男朋友面前哭了一场,真心觉得我只该吃屎,我好没用.哎.. 首先在上一篇记录了如何生成ssh-key,并使本地可以通过ssh的方式克隆和推送项目.但如果你有个github账号,有个 ...
- Hibernate5笔记8--Hibernate事务相关内容
Hibernate事务相关内容: (1) 事务四大特性(简称ACID): (1)原子性(Atomicity) 事务中的全部操作在数据库中是不可分割的,要么全部完成,要么均不执行. (2)一致性(Con ...
- 一个简单的java jdbc案例
有些时候,配置一个spring+mybatis框架,然后写xml,dao ,service显得特别繁琐. 如果我们只是想查一下数据库,不考虑连接复用也不考虑动态sql,可以用原生的jdbc来实现,方便 ...