#P3098. [USACO13DEC] The Bessie Shuffle G
[USACO13DEC] The Bessie Shuffle G
题目背景
征求翻译。如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献。
题目描述
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 <= 1,000,000,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.
50% of test cases will have N <= 100,000.
贝西正在练习她的纸牌魔术。她早已精通“贝西洗牌法”:一次M张牌的重组排序式得从顶上数第i张牌变为第P[i]张牌的洗牌方式。
现在贝西正在练习对大牌堆的洗牌。她有一摞总计N张牌(M<=N<=1,000,000,000)为方便而被从1至n标号。她以把前M张牌贝西洗牌的方式重组,将洗好的牌置于牌堆顶端,然后她将第一张牌从牌堆里拿出并将它面朝下放置。她重复这个过程直到无牌可用,成功地将顶端的牌码在(之前)每一张牌上。当贝西的牌数小于M之后,她不再洗牌,而是继续放置顶端的牌于其它牌上。
贝西知道牌堆最初是有序的,第一张在最上面,第二张其次,最后一张为第N张。给你“贝西式洗牌”的描述,帮助贝西计算哪张牌最终被放在牌堆内指定的位置Q
(1 <= Q <= N, Q <= 5,000)
50%的数据中,N <= 100,000.
输入格式
* 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).
第一行: 一行数据(然而不知道为何重复),包括被空格所分隔的N,M,Q。
第二行到第1+M行:第i+1行包括“贝西洗牌法”中的第i张牌自上而下的顺序与它的P[i]值(1 <= P[i] <= M).
第2+M行到第1+M+Q行:第i+1+M行包括一个描述对于第i个的问题的整数q_i 。你的任务是计算从上向下的第q_i张牌上的数字(原文为label)。
输出格式
* Lines 1..Q: On the i-th line, print a single integer indicating the card at position q_i from the top.
第1行到第Q行:在第i行输出一个整数即自上而下数第q_i张牌上的数字。
输入输出样例
5 3 5
3
1
2
1
2
3
4
5
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]
贝西的五张牌刚开始顺序为 [1, 2, 3, 4, 5]。她一次洗三张牌,效果是将第一张牌放到底部。以上五个问题询问了每一张牌的位置。
洗牌的顺序是:
[1, 2, 3, 4, 5] -> [2, 3, 1, 4, 5] (将2正面向下放置)
[3, 1, 4, 5] -> [1, 4, 3, 5] (将1正面向下放置)
[4, 3, 5] -> [3, 5, 4] (将3正面向下放置)
[5, 4] (将5正面向下放置)
[4] (将4正面向下放置)
这就形成了最终的顺序:[4, 5, 3, 1, 2]