D. A Leapfrog in the Array (思维)

题目

思路:经过模拟可以发现对于一个元素的移动每次移动量是之前一次的两倍,如果将列表倒过来从0开始,那么每次元素的位置移动便是x=2x+1,现在给出了最后的某个位置,那么我们只需逆转x=(x-1)/2直到位置为偶数就是开始移动的位置,因为列表倒置后所有元素位置会变为偶数,而每次进行x=2x+1又会变成奇数,所以逆移动到了偶数即是开始的位置。

Code:

#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;
const int Max = 5e6 + 5;
vector<int> vec[Max];

int main()
{
    
    
	ll n, q;cin >> n >> q;
	n = n * 2 - 1;
	while (q--)
	{
    
    
		ll t;cin >> t;
		if (t & 1)cout << (t + 1) / 2 << endl;
		else
		{
    
    
			t = n - t;
			while (t % 2 == 1)
			{
    
    
				t = (t - 1) / 2;
			}
			t=n - t;
			cout << (t + 1) / 2 << endl;
		}
	}
}

猜你喜欢

转载自blog.csdn.net/asbbv/article/details/114953271