Codeforces Round #469 (Div. 2) D. A Leapfrog in the Array(找规律)

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

比赛时理解错题意了,以为询问的是数字最终的位置,最后写完了过第二个例子的时候才发现不是.
贴份代码纪念一下把(也不知道对不对0.0)

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define pb push_back
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rep1(i,b,a) for(int i=b;i>=a;i--)
using namespace std;
const int N=2e5+100;
int arr[N];
vector<int>G[N];
int main()
{
    ll n,q;
    cin>>n>>q;
    while(q--)
    {
        ll a;
        cin>>a;
        ll j=(n-a+1)*2;
        j-=1;
        ll pre=1;
        a=a*2-1;
        ll k=a;
        while(1)
        {
            if(k<=n)
            {
                cout<<k<<endl;
                break;
            }
            //cout<<pre+j<<endl;
            k=a-j-pre;
            pre=j+pre;
            j*=2;
        }
    }
    return 0;
}

正解:打表找规律,
这是一份打表的代码,还是比较容易写的

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define pb push_back
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rep1(i,b,a) for(int i=b;i>=a;i--)
using namespace std;
const int N=2e5+100;
int arr[N];
vector<int>G[N];
int main()
{
    ll n,q;
    cin>>n;
    // mx=n;
    memset(arr,-1,sizeof arr);
    rep(i,1,2*n)
    {
        if((i+1)%2==0)
            arr[i]=(i+1)/2;
    }
    int i=2*n-1,j=2*n-2;
    while(j>=1)
    {
        while(arr[j]!=-1&&j>=1)
            j--;
        arr[j]=arr[i];
        i--;
    }
    rep(i,1,2*n)
    cout<<arr[i]<<' ';
    cout<<endl;
     j=n;
    while(j)
    {
        cout<<j*2<<endl;
        rep(i,1,2*n)
        {
            if(arr[i]==j)
                cout<<2*j-i<<' ';
        }
        cout<<endl;
        j--;
    }

    return 0;
}

找过规律之后发现,奇数下标存放的数是不发生移动的,偶数的移动也是有规律的,刚开始是n-x/2,之后就是前者/2,所以代码就出来了

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define pb push_back
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rep1(i,b,a) for(int i=b;i>=a;i--)
using namespace std;
const int N=2e5+100;
int arr[N];
vector<int>G[N];
int main()
{
    ll n, q, x;
    cin >> n >> q;
    while(q--)
    {
        cin >> x;
        if (x % 2)
        {
            cout << x/2+1 << endl;
        }
        else
        {
            ll t = n - x/2;
            while(t % 2 == 0)
            {
                x += t;
                t /= 2;
            }
            cout << (x+t)/2+1 << endl;
        }
    }
    return 0;
}
posted @ 2018-03-10 16:57  ffgcc  阅读(101)  评论(0编辑  收藏  举报