2019nanchang icpc网络赛E(双端队列+模拟)

题目链接:https://nanti.jisuanke.com/t/41352

John is not only a magic master but also a shuffling master.

Famous though he is, he likes interacting with his fans by playing a game with his fantastic shuffling skills.

The game shows as follows:

He first shows a deck of pokers contains NNN cards indexed 1,2,…,N1,2,\dots ,N1,2,…,N and all the cards are the same. He notes that the side contains numbers as the front side and the other side as the back. Later he selects one of his fans to choose a positive integer MMM that is not greater than 101010. After that, he will shuffle the cards purposefully and put it on the desktop. Note that at this moment the front sides of the cards are facing to the desk.

Then, the selected fans should perform the following steps until there is no card on the desk.

Place the card on the top of the piles to John's hand. If there are already some cards in his hand, put it on the top of them. 
If there remain cards on the desk:

(a) Put a card from the top of the piles to the bottom of it.

(b) Execute the step (aaa) of MMM times.

© Go to step 111.

Next, it is time to witness the miracle.

John will continually overturn the cards on his hand top to bottom, and we will find that the cards are always in decreasing order.

One day, one of John’s fans, Tom, a smart JBer, understands the key to this magic. He turns to you and comments that the key to that magic is the shuffling John did previously. For any number MMM, he will turn the cards in a specific order after the shuffling. As you are not as smart as Tom, you should find the index of the KKKth cards from the top of the piles after the shuffling.
Input

The first line contain a positive integer T(1≤T≤10)T (1\le T \le10)T(1≤T≤10) – the number of test cases.

For each test cases, the first line contain a positive integer N(1≤N≤40000000)N (1 \le N \le 40000000)N(1≤N≤40000000) , indicating the numberof cards.

The second line contain a positive integer M(1≤M≤10)M (1\le M \le 10)M(1≤M≤10) – the number the fans select.

The third line contain an integer Q(1≤Q≤100)Q (1 \le Q \le 100)Q(1≤Q≤100) – indicating that there are QQQ questions.

Next, there are QQQ lines, each with an integer K(1≤K≤N)K (1 \le K \le N)K(1≤K≤N) – representing a query.
Output

For each query, you should output the index of the KKKth cards from the top of the piles after the shuffling.
样例输入

1
5
1
2
2
3

样例输出

5
2

样例解释

Sample description - The indexs of the cards after the shuffling are: 1 5 2 4 3152431 5 2 4 3 (Left is top)


分析:

与约瑟夫环有点像,但求的东西不一样;这题是利用双端队列模拟
注意点:对于不在查询数组a中的数,mp不必保存,不然会内存超限

代码:
#include <bits/stdc++.h>

using namespace std;
#define ll long long 
#define fr first
#define sc second
deque<int> que;
map<int, int> mp,bb;

int a[110];
int main()
{
// push_back(x)/push_front(x) //把x压入后/前端
// back()/front() //访问(不删除)后/前端元素
// pop_back() pop_front() //删除后/前端元素
// empty() //判断deque是否空
// size() //返回deque的元素数量
// clear() //清空deque
// 支持通过sort(d.begin(),d.end())进行排序。
	int t;
	int n, m,q,k;
	scanf("%d",&t);
	while(t--)
	{
		
		mp.clear();
		que.clear();
		bb.clear();
		scanf("%d%d%d",&n,&m,&q);
		int mx=0;
		// cout<<n<<m<<q<<endl;
		for(int i=0;i<q;i++)
		{
			scanf("%d",&a[i]);
			bb[a[i]]=i;
			// mx=max(a[i],mx);
		}
		int v=0;
		int pos;
		que.push_front(n);
		
		for(int i=n-1;i>=1;i--)
		{
			v++;
			
			int tt=m%v;
			// cout<<tt<<endl;
			while(tt--)
			{
				
				pos=que.back();
				// cout<<pos<<endl;
				que.pop_back();
				que.push_front(pos);
				// cout<<pos<<endl;
			}
			
			que.push_front(i);
		}
		int tmp=1;
		
		int xx=0;
		for(int i=0;i<q;i++)
		{
			if(a[i]<tmp)
				printf("%d\n",mp[a[i]]);
			else
			{
				while(a[i]>=tmp)
				{
					if(bb.count(tmp)) 
						mp[tmp]=que.front();
					que.pop_front();
					tmp++;
				}
				printf("%d\n",mp[a[i]]);
			}
		}

	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值