zoukankan      html  css  js  c++  java
  • CodeForces

    ##CodeForces - 949B

    题意:现在给你一个n,表示有2*n-1个方格,第奇数方格上会有一个数字 1-n按顺序放。第偶数个方格上是没有数字的。变动规则是排在最后一个位置的数字,移动到它前边最近的空位 。 直到数字之间没有空位。最终的数列是由n已经确定的。给你q,表示q次查询,每次查询输入一个x,问数列第x位上的数字是多少? .img

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

    当n=13时,数列为[1, 12, 2, 8, 3, 11, 4, 9, 5, 13, 6, 10, 7].

    题意:

    中文的我想大家都看得懂

    分析:

    假设现在询问的位置是x,1<=x<=nx,且(1<=x<=n)

    奇数位置的值一定能立马确定为(x+1)/2(x+1)/2,因为奇数位置刚开始都有值且确定,往前移动只能移动到偶数位置上

    • 如果xx是奇数

      val=(x+1)/2val=(x+1)/2​

    • xx是偶数

      xvalvalxx假设最后x数的位置的数为val,那么我们回到val刚放到x这个位置的状态,此时位置x之前的偶数位置一定是空的,xx/2valxxnx/21即位置x前面有x/2个数,又因为val刚放到x这个位置,故x后面的数一定是连续的数共有n-x/2-1个,

      valxx+(nx/21)+1那么val放到位置x之前一定在位置x+(n-x/2-1)+1的位置上xval=(x+1)/2,x如果此时x是奇数,则val=(x+1)/2,否则重复x为偶数的状态x直到x为奇数即可​

    #include<cstdio>
    #include<algorithm>
    #include<stack>
    #include<queue>
    #include<map>
    #include<set>
    #include<cmath>
    #include<vector>
    #include<cstring>
    #include<string>
    #include<iostream>
    #include<iomanip>
    #define mset(a,b)   memset(a,b,sizeof(a))
    using namespace std;
    typedef unsigned long long ull;
    typedef long long ll;
    const int maxn=1e3+100;
    const int branch=26;
    const int inf=0x7fffffff;
    int main()
    {
        ll n,q,x;
        ios::sync_with_stdio(false);/*这语句加速cin输入,去了也没关系*/
        cin>>n>>q;
        while(q--)
        {
            cin>>x;
            while(x%2==0)
            {
                x=n+x/2;
            }
            cout<<(x+1)/2<<endl;
        }
        return 0;
    }
    
  • 相关阅读:
    php模拟http请求的方法
    快递100接口开发
    live555从RTSP服务器读取数据到使用接收到的数据流程分析
    VLC源码分析知识总结
    VLC播放器架构剖析
    Android Audio System 之一:AudioTrack如何与AudioFlinger
    VLC各个Module模块之间共享变量的实现方法
    流媒体开发之--HLS--M3U8解析(2): HLS草案
    M3U8格式讲解及实际应用分析
    通用线程:POSIX 线程详解,第 3 部分 条件互斥量(pthread_cond_t)
  • 原文地址:https://www.cnblogs.com/dchnzlh/p/10427232.html
Copyright © 2011-2022 走看看