zoukankan      html  css  js  c++  java
  • Codeforces 950 D A Leapfrog in the Array

    Discription

    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 iis 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 ≤ 10181 ≤ 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.

    Example

    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].

    找规律题。

    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    ll N,pos;
    int Q,tp;
    
    inline void solve(){
        tp=1;
        ll base=0,lef=N;
        while(1){
            if(!((tp^pos)&1)){
                printf("%lld
    ",base+(pos+tp)/2);
                return;
            }
            if(tp){
                tp=(lef&1)?0:1;
                base+=(lef+1)>>1;
                lef>>=1,pos>>=1;
            }
            else{
                tp=(lef&1)?1:0;
                base+=lef>>1;
                lef=(lef+1)>>1;
                pos=(pos+1)>>1;
            }
        }
    }
    
    int main(){
        scanf("%lld%d",&N,&Q);
        while(Q--){
            scanf("%lld",&pos);
            solve();
        }
        return 0;
    }
    

      

  • 相关阅读:
    js之iframe子页面与父页面通信
    PHP安全编程:HTTP请求欺骗
    PHP安全编程:防止SQL注入
    PHP多种序列化/反序列化的方法
    empty(),isset()与is_null()的实例测试
    &nbsp|&quot|&amp|&lt|&gt等html字符转义
    手把手叫你SQL注入攻防(PHP语法)
    HTTP协议详解
    Cache缓存机制与文件缓存原理PHP2
    数据变成了真正的生产资料,而且是人类第一次没有依赖大自然,单纯依靠自身行为获得的生产资料。
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8625683.html
Copyright © 2011-2022 走看看