题意:

你正在使用的音乐播放器有一个所谓的乱序功能,即随机打乱歌曲的播放顺序。
假设一共有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. ffmpeg的API函数用法 :sws_scale函数的用法-具体应用

    移植ffmpeg过程中,遇到swscale的用法问题,所以查到这篇文章.文章虽然已经过去很长时间,但是还有颇多可以借鉴之处.谢谢“咕咕钟. 转自:http://guguclock.blogspot.c ...

  2. 【转】MYSQL数据库设计规范与原则

    转载出:http://www.cnblogs.com/lovekingly/p/5044278.htmlMYSQL数据库设计规范 1.数据库命名规范 采用26个英文字母(区分大小写)和0-9的自然数( ...

  3. Spring MVC处理响应的 header

    我们经常需要在HttpResponse中设置一些headers,我们使用Spring MVC框架的时候我们如何给Response设置Header呢? So easy, 看下面的代码: @Request ...

  4. TPL概要

    ReaderWriterLockSlim.读写锁,允许多个线程同时获取读锁,但同一时间只允许一个线程获得写锁,因此也称作共享-独占锁.只有等到对象被写入锁占用的时候,才会阻塞 Barrier .它允许 ...

  5. codeforces15E Triangles

    传送门:http://codeforces.com/problemset/problem/15/E [题解] # include <bits/stdc++.h> using namespa ...

  6. HDU1693 Eat the Trees(zerojudge a228)

    传送门: https://zerojudge.tw/ShowProblem?problemid=a228 http://acm.hdu.edu.cn/showproblem.php?pid=1693 ...

  7. python程序练习题集

    1.#输入a,b,c,d4个整数,计算a+b-c*d的结果 a=input("please input a nimber:") b=input("please input ...

  8. 关于项目中根据当前数据库中最大ID生成下一个ID问题——(五)

    1.关于部门管理时候根据上级产生下级部门ID的问题(传入一个参数是上级部门id)

  9. 【黑客免杀攻防】读书笔记18-最终章Anti Rootkit

    1.免杀技巧的遏制 1.1.PE文件 入口点不在第一个区段或在最后一个区段 入口点处代码附近只有一小段代码 入口点在正常范围之外 入口点为一个无效的值,实际入口点为TLS的入口点 区段名重复或者不属于 ...

  10. 使用Python扫描网络MAC地址对应的IP地址

    #!/usr/bin/env python # -*- coding: utf-8 -*- from scapy.all import srp,Ether,ARP,conf ipscan='192.1 ...