codeforces 950D. A Leapfrog in the Array

链接: http://codeforces.com/contest/950/problem/D

题意: 现在给你一个n 然后一个序列1 0 2 0 3 0 4 0 5 0 6 0 7 0 8  现在每次讲后边第一个非零的数移到后边第一个为零的位置,直到不能操作为止,现在q个询问,每次问你p位置应该是什么。

思路:自己画一下,就可以看出来,每个空位置是由他后边某一个固定的位置跳过来的,那么我可以直接跳回去找到那个值就好了。

代码:

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

ll n;
int q;
ll p;
ll x;
ll len;

int main()
{
    cin>>n>>q;
    while(q--)
    {
        scanf("%lld",&p);
        if(p&1){
            p+=1;
            p/=2;
            printf("%lld\n",p);
        }
        else{
            len=n-p/2;
            while(p%2==0)
            {
                p+=len;
                len/=2;
            }
            p++;
            p/=2;
            printf("%lld\n",p);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yjt9299/article/details/82863940