首页 > 其他分享 >[USACO13DEC] The Bessie Shuffle S

[USACO13DEC] The Bessie Shuffle S

时间:2023-07-26 20:45:06浏览次数:49  
标签:shuffle Shuffle 贝西 样例 USACO13DEC put Bessie card

[USACO13DEC] The Bessie Shuffle S

目录

[P3095 USACO13DEC] The Bessie Shuffle S - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

题目描述

Bessie is practicing her card tricks. She has already mastered the Bessie- shuffle -- a shuffle on M (2 <= M <= 100,000) cards that reorganizes the cards so the i-th card from the top is now the P[i]-th card from the top.

Now Bessie is practicing shuffles on larger decks. She has a deck of N cards (M <= N <= 100,000) conveniently labeled 1 to N. She shuffles this deck by taking the first M cards and performing the Bessie-shuffle on them, placing the shuffled cards back on top of the deck. She then removes the top card from the deck and places it face down. She repeats this process, placing the top cards successively on top of each other, until she is out of cards. When Bessie has less than M cards left, she no longer performs the Bessie-shuffle, but continues to place the top card on top of the others.

Bessie knows that the deck initially started in sorted order, with 1 on top, 2 next, and N on the bottom. Given the description of the Bessie-shuffle, help Bessie compute which cards end up located at Q different specified positions (1 <= Q <= N, Q <= 5,000) in the deck.

贝西有一种独门的洗牌方法,称为贝西洗A:

贝西洗A:将一堆共M (2 <= M <= 100,000)张从上到下编号1..M的纸牌,从上到下第i张牌洗到位置p[i]。例M=3,P[1]=3,p[2]=1,p[3]=2,则执行一次洗法A后,从上到下将变为2,3,1,即牌1放到位置3,牌2放到位置1,牌3放到位置2。

贝西现在要练习另外一种更强的洗牌方法,称为贝西洗B,他有一堆N (M <= N <= 100,000)张编号为1..N的牌,并按从上到下1到N的顺序堆放。另有一个堆用来辅助洗牌,称为临时堆,开始时为空。

贝西洗B:(1)将最上面M张牌进行贝西洗A,(2)将最上面的一张牌放到临时堆的最上方;重复(1)(2)操作,直到原先的堆没有牌为止。

以上过程中,当原先堆的牌不足M张的时候,将不进行贝西洗A,而是将最上面的牌依次放到临时堆上。

现在有Q个询问,求临时堆中Qi位置上的牌的编号。

输入格式

* Line 1: A single line containing N, M and Q separated by a space.

* Lines 2..1+M: Line i+1 indicates the position from the top, P[i], of the i-th card in the Bessie-shuffle (1 <= P[i] <= M).

* Lines 2+M..1+M+Q: Line i+1+M contains a single integer q_i

describing the i-th query. You are to compute the label on the card located in position q_i from the top (1 <= q_i <= N).

输出格式

* Lines 1..Q: On the i-th line, print a single integer indicating the card at position q_i from the top.

样例 #1

样例输入 #1

5 3 5 
3 
1 
2 
1 
2 
3 
4 
5

样例输出 #1

4 
5 
3 
1 
2

提示

Bessie has a deck of 5 cards initially ordered as [1, 2, 3, 4, 5]. Her shuffle is on 3 cards and has the effect of moving the top card to the bottom. There are 5 queries querying each position in the deck.

The shuffle proceeds as:

[1, 2, 3, 4, 5] -> [2, 3, 1, 4, 5] (put 2 face down) 
[3, 1, 4, 5] -> [1, 4, 3, 5] (put 1 face down) 
[4, 3, 5] -> [3, 5, 4] (put 3 face down) 
[5, 4] (put 5 face down) 
[4] (put 4 face down) 

This produces the final order of [4, 5, 3, 1, 2]

思路

从后往前模拟第 \(x\) 张牌每一轮之前的位置。

code

#include <bits/stdc++.h>
#define fu(x , y , z) for(int x = y ; x <= z ; x ++)
using namespace std;
int n , m , rep[100005];
int main () {
    int T , a , ans;
    scanf ("%d%d%d" , &n , &m , &T);
    fu (i , 1 , m) {
        scanf ("%d" , &a);
        rep[a] = i;
    }
    while (T --) {
        scanf ("%d" , &a);
        ans = n - a + 1;
        for (int i = n - a + 1 ; i >= 1 ; i --) 
            if (ans <= i + m - 1 && i + m - 1 <= n) 
                ans = i + rep[ans - i + 1] - 1;
        printf ("%d\n" , ans);
    }
    return 0;
}

标签:shuffle,Shuffle,贝西,样例,USACO13DEC,put,Bessie,card
From: https://www.cnblogs.com/2020fengziyang/p/17583500.html

相关文章

  • org.apache.spark.shuffle.FetchFailedException: The relative remote executor(Id:
    问题描述org.apache.spark.shuffle.FetchFailedException:Therelativeremoteexecutor(Id:21),whichmaintainstheblockdatatofetchisdead.最近在做Spark的性能优化,测试使用不同CPU核数和内存对计算性能的影响,由于是在测试集群进行测试的,硬件配置比生产上面的要少和......
  • Shuffle Cards (牛客多校) (rope 块状链表 用作可持续优化平衡树, 用于区间的整体移动
    rope:#include<ext/rope>usingnamespace__gnu_cxx; 定义方法:rope<变量类型>变量名称;人话解释:超级string算法解释:块状链表(即讲链表与数组的优势结合,形成分块思想)用途解释:这本来是一个用于快速操作string的工具,却一般被定义成int,然后用作可持久化线段树!insert(intpos,s......
  • JAVA:Collections类的shuffle()方法
    Java.util.Collections类下有一个静态的shuffle()方法,如下:1)staticvoidshuffle(List<?>list)使用默认随机源对列表进行置换,所有置换发生的可能性都是大致相等的。2)staticvoidshuffle(List<?>list,Randomrand)使用指定的随机源对指定列表进行置换,所有置换发生的可能性......
  • C++黑马程序员——P251-254. 常用排序算法 sort,random_shuffle,merge,reverse
    P251.常用排序算法——sortP252....——random_shuffleP253....——mergeP254....——reverseP251.sort  1#include<iostream>2#include<vector>3#include<algorithm>4#include<functional>//用greater5usingnamespacest......
  • MapReduce Shuffle源码解读
    MapReduceShuffle源码解读相信很多小伙伴都背过shuffle的八股文,但一直不是很理解shuffle的过程,这次我通过源码来解读下shuffle过程,加深对shuffle的理解,但是我自己还是个......
  • 针对sarasa-shuffle.woff2加密字体进行解密
    本文针对的是类似于sarasa-shuffle.woff2加密字体的一个研究。字体加密是使用Unicode编码将其映射到不同的字体显示的一种前端显示加密手段。在反爬虫中能够起到较好的效......
  • 第23课:Spark旧版本中性能调优之HashShuffle剖析及调优
    第23课:Spark旧版本中性能调优之HashShuffle剖析及调优2个core表示2个并行度文件个数:cpucores*reducestasksspark.shuffle.consolidateFiles=trueHashShuffle在spark中......
  • Spark系列 - (5) Spark Shuffle
    目前已经更新完《Java并发编程》,《JVM性能优化》,《Spring核心知识》《Docker教程》和《Spark基础知识》,都是多年面试总结。欢迎关注【后端精进之路】,轻松阅读全部文章。......
  • 「CF1392H」ZS Shuffles Cards
    题目点这里看题目。你有\(n+m\)张牌,其中有恰好\(n\)张为数字牌,分别标有\(1,2,3,\dots,n\),剩下的恰好\(m\)张均为鬼牌。一开始,牌被随机打乱,同时你有一个集合\(......
  • np.random.seed np.random.shuffle 组合使用
     importnumpyasnpnum_train=10indices=list(range(num_train))print(indices)print(len(indices))np.random.seed(2)np.random.shuffle(indices)pri......