zoukankan      html  css  js  c++  java
  • CodeForces 950D-A Leapfrog in the Array(打表找规律)

    题目链接: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)+1leftarrow(9,2)+1leftarrow(8,0)+1=(8,8)+1leftarrow(7,6)+1leftarrow(6,4)+1leftarrow(5,2)+1leftarrow(4,4)+1leftarrow(3,2)+1leftarrow(2,2)+1leftarrow(1,1)+1),一旦位置被确定位奇数了就可以返回了,所以就可以这么往上推了。

    其模拟片段如下:

    ll ans=0,n1=n;
    while (!(x&1)) {
    	ll m=x/2;
    	x=n1-m;
    	n1-=m;
    	ans+=m;
    }
    printf("%lld
    ",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
    ",(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
    ",ans+((x+1)>>1LL));
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    单例模式
    spring boot 调度任务
    linux yum安装MySQL5.6
    day5模块学习--sys模块
    day5模块学习 -- os模块学习
    day5模块学习--random模块
    day5模块学习 -- time、datetime时间模块
    生成随机验证码的方法
    SQL中format()函数对应的格式
    day4作业小代码练习
  • 原文地址:https://www.cnblogs.com/lonely-wind-/p/13454001.html
Copyright © 2011-2022 走看看