当前位置 博文首页 > 文章内容

    CodeForces 950D-A Leapfrog in the Array(打表找规律)

    作者: 栏目:未分类 时间:2020-08-07 17:15:26

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    题目链接:https://codeforces.com/problemset/problem/950/D
    CSDN食用链接:https://blog.csdn.net/qq_43906000/article/details/107865886

    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:
    https://espresso.codeforces.com/353e66df2b64f64592e4ec61c819b5ad3856d759.png
    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 ≤ 10^{18}, 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 \(x _i (1 ≤ x_ i ≤ 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
    Input
    4 3
    2
    3
    4
    Output
    3
    2
    4

    Input
    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,每个数之间刚开始有个间隙,现在每次将最后一个元素填入最后的空缺中,最后得到一个完整的前n个数的序列,现在问你对于n个这样操作后第x个位置的值是什么,询问有q个。

    emmm,如果位置是奇数的话位置就是确定的,即\((pos+1)/2\),如果是偶数呢?emmm,感觉不太会。。。没关系,我们可以打个表来找找规律,对于前20个数其1到n的排列如下:

    1: 1
    2: 1 2
    3: 1 3 2
    4: 1 3 2 4
    5: 1 5 2 4 3
    6: 1 4 2 6 3 5
    7: 1 6 2 5 3 7 4
    8: 1 5 2 7 3 6 4 8
    9: 1 9 2 6 3 8 4 7 5
    10: 1 6 2 10 3 7 4 9 5 8
    11: 1 9 2 7 3 11 4 8 5 10 6
    12: 1 7 2 10 3 8 4 12 5 9 6 11
    13: 1 12 2 8 3 11 4 9 5 13 6 10 7
    14: 1 8 2 13 3 9 4 12 5 10 6 14 7 11
    15: 1 12 2 9 3 14 4 10 5 13 6 11 7 15 8
    16: 1 9 2 13 3 10 4 15 5 11 6 14 7 12 8 16
    17: 1 17 2 10 3 14 4 11 5 16 6 12 7 15 8 13 9
    18: 1 10 2 18 3 11 4 15 5 12 6 17 7 13 8 16 9 14
    19: 1 15 2 11 3 19 4 12 5 16 6 13 7 18 8 14 9 17 10
    20: 1 11 2 16 3 12 4 20 5 13 6 17 7 14 8 19 9 15 10 18

    emmm,似乎发现了什么不得了的东西。也就是说每一行可以由上一行推出来的。假设求的是第13行的第10位,那么他是由第12行的第8位+1得来的,而第12行的第八位是由第11行的第6位+1得来\(\leftarrow (10,4)+1\leftarrow(9,2)+1\leftarrow(8,0)+1=(8,8)+1\leftarrow(7,6)+1\leftarrow(6,4)+1\leftarrow(5,2)+1\leftarrow(4,4)+1\leftarrow(3,2)+1\leftarrow(2,2)+1\leftarrow(1,1)+1\),一旦位置被确定位奇数了就可以返回了,所以就可以这么往上推了。

    其模拟片段如下:

    ll ans=0,n1=n;
    while (!(x&1)) {
    	ll m=x/2;
    	x=n1-m;
    	n1-=m;
    	ans+=m;
    }
    printf("%lld\n",ans+((x+1)>>1LL));
    

    以下是AC代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    typedef long long ll;
    const int mac=2e5+10;
    
    int main(int argc, char const *argv[])
    {
    	ll n,q;
    	scanf ("%lld%lld",&n,&q);
    	while (q--){
    		ll x;
    		scanf ("%lld",&x);
    		if (x&1) printf("%lld\n",(x+1)>>1LL);
    		else {
    			ll ans=0,n1=n;
    			while (!(x&1)){
    				ll m=x/2;
    				x=n1-m; n1-=m;
    				ans+=m;
    			}
    			printf("%lld\n",ans+((x+1)>>1LL));
    		}
    	}
    	return 0;
    }