D. A Leapfrog in the Array (思维) 冷不防 2021-07-16 21:55 319阅读 0赞 [题目][Link 1] 思路:经过模拟可以发现对于一个元素的移动每次移动量是之前一次的两倍,如果将列表倒过来从0开始,那么每次元素的位置移动便是x=2*x+1,现在给出了最后的某个位置,那么我们只需逆转x=(x-1)/2直到位置为偶数就是开始移动的位置,因为列表倒置后所有元素位置会变为偶数,而每次进行x=2*x+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; } } } [Link 1]: https://codeforc.es/problemset/problem/950/D
还没有评论,来说两句吧...