Codeforces Round #469 (Div. 2)D. A Leapfrog in the Array(递推)

D. A Leapfrog in the Array
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm.

Let’s consider that initially array contains n numbers from 1 to n and the number i is located in the cell with the index 2i - 1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all n numbers will appear in the first n cells of the array. For example if n = 4, the array is changing as follows:

You have to write a program that allows you to determine what number will be in the cell with index x (1 ≤ x ≤ n) after Dima’s algorithm finishes.

Input
The first line contains two integers n and q (1 ≤ n ≤ 1018, 1 ≤ q ≤ 200 000), the number of elements in the array and the number of queries for which it is needed to find the answer.

Next q lines contain integers xi (1 ≤ xi ≤ n), the indices of cells for which it is necessary to output their content after Dima’s algorithm finishes.

Output
For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima’s algorithm finishes.

Examples
inputCopy
4 3
2
3
4
output
3
2
4
inputCopy
13 4
10
5
4
8
output
13
3
8
9
Note
The first example is shown in the picture.

In the second example the final array is [1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7].

题意:起初,1~n被顺序排列2n-1个格子中(两两之间相隔一个空格),每一次最后面的数字会跳到最近的空格之中。最终当不能继续跳后,有q次询问,问在x位置上的数字是多少。
思路:对于最终位置x上的数,我们可以反推出初始状态的位置。
举例第2个样例的第一次询问:假设10号位上一次所在的位置为m,那么当m跳到10之后,还有x个元素会跳到10前面,x应该是10以前的空位,那么推出m=x+n+1.这里m=18.
再推18之前在那个位置,显然,当18与10之间的空位都被填满后,才轮到18跳,设18之前仍为m,10到18之间有x个空位,那么m=x+18+1.m=22.
递推下去…最终m=25.那么答案为(25+1)/2=13.
对于每个位置,我们都可以这样反推回去,得到答案,对于题目,每个数只会跳到最近的空位(偶数位位上),那么如果推出某个数在奇数上,那么显然这是其初始的位置。
奇怪的复杂度,反正过了,哈哈。
代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#define met(s,k) memset(s,k,sizeof s)
#define scan(a) scanf("%d",&a)
#define scanl(a) scanf("%lld",&a)
#define scann(a,b) scanf("%d%d",&a,&b)
#define scannl(a,b) scanf("%lld%lld",&a,&b)
#define scannn(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define prin(a) printf("%d\n",a)
#define prinl(a) printf("%lld\n",a)
using namespace std;
typedef long long ll;
const  int maxn=2e6+10;
ll n,q;
ll get(ll now, ll last)
{
    if(last%2)return (now-last)/2;
    else return (now-last)/2-1;
}
int main()
{
    scannl(n,q);
    for(int i=0;i<q;i++)
    {
        ll x,pos;
        scanl(x);
        if(x%2)
        {
            prinl((x+1)/2);
            continue;
        }
        ll now=x,last=1;
        now=get(now,last)+n+1;
        last=x;
        while (pos=get(now,last)&&now%2==0)
        {
            ll temp=now;
            now=now+get(now,last)+1;
            last=temp;
        }
        prinl((now+2)/2);
    }
    return  0;
}

猜你喜欢

转载自blog.csdn.net/swust5120160705/article/details/79505368