博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforces 949B A Leapfrog in the Array
阅读量:4459 次
发布时间:2019-06-08

本文共 3334 字,大约阅读时间需要 11 分钟。

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
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
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 else36 return seq/2+1;37 }38 else39 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
N||(now&1)){48 printf("%I64d\n",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

 

 

 

 

转载于:https://www.cnblogs.com/lzhAFO/p/8536647.html

你可能感兴趣的文章
C++ Primer 第六章 函数
查看>>
交互设计算法基础(3) - Quick Sort
查看>>
Ubuntu各种软件的安装
查看>>
智能社的邀请码
查看>>
算法与分析 统计数字问题和整数因子分解问题?
查看>>
变量提升
查看>>
谜题88:原生类型的处理
查看>>
ajax 415 错误 $.ajax 中的contentType
查看>>
【CodeForces】191C Fools and Roads
查看>>
enum hack
查看>>
Visual Studio 2008切换到设计视图卡死解决办法-Troubleshooting "Visual Studio 2008 Design view hangs" issues...
查看>>
数据库设计范式
查看>>
sql2005-数据库备份方案 (转载)
查看>>
centos中安装jdk的操作
查看>>
selenium--等待的三种方式
查看>>
android中自定义的dialog中的EditText无法弹出输入法解决方案
查看>>
Android Activity整体管理和关闭工具类封装
查看>>
nginx 安装
查看>>
selenium下拉一个框内的滚动条
查看>>
跟老邓一起学Windows Phone7开发(一)第一个程序
查看>>