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属于不断被优化和改进的代码库的一部分,从许多 ...
随机推荐
- Vue.js学习笔记(二)
Vue.js不支持IE8及以下的版本,因为vue使用了IE8无法模拟的ECMAScript5的特性,它支持所有兼容ECMAScript5的浏览器. <!DOCTYPE html> < ...
- BFS:八数码问题
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> ...
- IOC轻量级框架之Autofac
http://www.cnblogs.com/WeiGe/p/3871451.html http://www.cnblogs.com/hkncd/archive/2012/11/21/2780041. ...
- [HAOI2006]旅行 题解(kruskal)
[HAOI2006]旅行 Description Z小镇是一个景色宜人的地方,吸引来自各地的观光客来此旅游观光.Z小镇附近共有N个景点(编号为1,2,3,-,N),这些景点被M条道路连接着,所有道路都 ...
- 20、List集合中特有的方法
List里面的特有方法简介 List中除了Collection里面的方法以外,内部还有一些方法,通过这些方法,开发者可以更方便的操作List接口的实现类. package com.monkey1024 ...
- [Openwrt扩展中篇]添加Aria2和webui
上一篇说了我构建了简单的网络硬盘,这一篇说的是我构造的aria2和webui,大概是这样我觉得有了网络硬盘,那么我是不是可以远程下载呢,翻阅了网上资料发现迅雷的Xware貌似不更新了,然后我发现了ar ...
- tensorflow环境安装
tensorflow环境安装1.安装虚拟机Virtrualbox下载地址:https://www.virtualbox.org/wiki/Downloads 2.下载安装Ubuntu镜像下载地址:ht ...
- UNIX环境高级编程 第6章 系统数据文件和信息
UNIX系统的正常运作需要用到大量与系统有关的数据文件,例如系统用户账号.用户密码.用户组等文件.出于历史原因,这些数据文件都是ASCII文本文件,并且使用标准I/O库函数来读取. 口令文件 /etc ...
- HTTP::Request 用 add-content 添加 POST参数
sub MAIN($cmd) { use HTTP::UserAgent; my $r = HTTP::Request.new(); $r.uri: 'http://localhost/a.php'; ...
- Linux MySQl 5.7.17 MySQL ERROR 1366(HY000):Incorrect string value 解决方法
MySQL ERROR 1366(HY000):Incorrect string value,在往数据库中插入中文的时候会出现. 这也就是编码问题,网上大部分都是说设置下配置文件中的设置,而可悲的是在 ...