B Card Trick

B. Card Trick
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Monocarp has just learned a new card trick, and can’t wait to present it to you. He shows you the entire deck of n cards. You see that the values of cards from the topmost to the bottommost are integers a1,a2,…,an, and all values are different.

Then he asks you to shuffle the deck m times. With the j-th shuffle, you should take bj topmost cards and move them under the remaining (n−bj) cards without changing the order.

And then, using some magic, Monocarp tells you the topmost card of the deck. However, you are not really buying that magic. You tell him that you know the topmost card yourself. Can you surprise Monocarp and tell him the topmost card before he shows it?

Input
The first line contains a single integer t (1≤t≤104) — the number of testcases.

The first line of each testcase contains a single integer n (2≤n≤2⋅105) — the number of cards in the deck.

The second line contains n pairwise distinct integers a1,a2,…,an (1≤ai≤n) — the values of the cards.

The third line contains a single integer m (1≤m≤2⋅105) — the number of shuffles.

The fourth line contains m integers b1,b2,…,bm (1≤bj≤n−1) — the amount of cards that are moved on the j-th shuffle.

The sum of n over all testcases doesn’t exceed 2⋅105. The sum of m over all testcases doesn’t exceed 2⋅105.

Output
For each testcase, print a single integer — the value of the card on the top of the deck after the deck is shuffled m times.

Example
inputCopy
3
2
1 2
3
1 1 1
4
3 1 4 2
2
3 1
5
2 1 5 4 3
5
3 2 1 2 1
outputCopy
2
3
3
Note
In the first testcase, each shuffle effectively swaps two cards. After three swaps, the deck will be [2,1].

In the second testcase, the second shuffle cancels what the first shuffle did. First, three topmost cards went underneath the last card, then that card went back below the remaining three cards. So the deck remained unchanged from the initial one — the topmost card has value 3.
将数组往后排等同于将数组拼接成一个无穷长度的循环数组

#include<bits/stdc++.h>
using namespace std;
int t, n, m, a[200000], b,i,j,c[200000],x=0;
int main() {
	cin >> t;
	while (t--) {		
		cin >> n;
		for (i = 0; i < n; i++)cin >> a[i];
		cin >> m;
		for (j = 0; j < m; j++) {
			cin >> b;
			if (j == 0) x = b;
			else{
				if (b + x < n) x = x + b;//未超出往后推
				else x=x+b-n;//超出回到起始位置
			}			
		}
		cout << a[x] << endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值