• codeforces 949B A Leapfrog in the Array


    B. A Leapfrog in the Array
    time limit per test
    2 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard 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
    input
    Copy
    4 3
    2
    3
    4
    output
    3
    2
    4
    input
    Copy
    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].

    题意:题中四张图已经说明题意

    数组中隔一个放个数字,然后找最后一个往最近的空子里塞

    q个询问,求最后在某个位置上的数字是什么。

    题解:把过程倒过来考虑:

    从最终状态开始,按照规则把被塞进的数移动回数组最后。

    就是按照4,3,2,1的顺序看题目中的图,递归模拟,注意细节。

     1 /*
     2 Welcome Hacking
     3 Wish You High Rating
     4 */
     5 #include<iostream>
     6 #include<cstdio>
     7 #include<cstring>
     8 #include<ctime>
     9 #include<cstdlib>
    10 #include<algorithm>
    11 #include<cmath>
    12 #include<string>
    13 using namespace std;
    14 int read(){
    15     int xx=0,ff=1;char ch=getchar();
    16     while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
    17     while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
    18     return xx*ff;
    19 }
    20 long long READ(){
    21     long long xx=0,ff=1;char ch=getchar();
    22     while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
    23     while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
    24     return xx*ff;
    25 }
    26 long long N,q;
    27 int Q;
    28 inline long long can(long long a,long long b){//计算在连续区间内有几个元素是需要移动的(下标为偶数)
    29     long long seq=(b-a+1);
    30     if(seq<=0)
    31         return 0;
    32     if(seq&1){
    33         if(b&1)
    34             return seq/2;
    35         else
    36             return seq/2+1;
    37     }
    38     else
    39         return seq/2;
    40 }
    41 void dfs(long long now,long long L,long long R){//now:询问元素所在的位置 L:可能移动的连续区间左端点 R:右端点
    42     long long t=R+can(L,R);
    43     if(R<now)
    44         dfs(now,R+1,t);
    45     else{
    46         long long np=can(L,now-1)+R+1;//np:now移动后应该在的位置
    47         if(np>N||(now&1)){
    48             printf("%I64d
    ",now/2+1);
    49             return;
    50         }
    51         dfs(np,now+1,np);
    52     }
    53 }
    54 int main(){
    55     //freopen("in","r",stdin);
    56     N=READ()*2-1,Q=read();
    57     for(int i=1;i<=Q;i++){
    58         q=READ();
    59         long long r=N/2+1;
    60         dfs(q,1,r);
    61     }
    62     return 0;
    63 }
    View Code
  • 相关阅读:
    C
    A
    hdu 三部曲1 Popular Cows tarjan算法&&缩点&&拓扑排序
    hdu 三部曲 Going Home 最小费用最大流 EK算法
    hdu 三部曲 1Minimum Cost 最小费用最大流EK算法
    hdu 三部曲1 Is the Information Reliable? 差分约束 bellman_ford算法
    hdu 三部曲1 Intervals 差分约束问题 spfa算法
    hdu 三部曲 Crashing Robots
    hdu 三部曲2 Rebuilding Roads
    Codeforces 1277C As Simple as One and Two
  • 原文地址:https://www.cnblogs.com/lzhAFO/p/8536647.html
Copyright © 2020-2023  润新知