codeforces 949B A Leapfrog in the Array
2 seconds
512 megabytes
standard input
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.
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.
For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes.
4 3
2
3
4
3
2
4
13 4
10
5
4
8
13
3
8
9
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的顺序看题目中的图,递归模拟,注意细节。
/*
Welcome Hacking
Wish You High Rating
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int read(){
int xx=,ff=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')ff=-;ch=getchar();}
while(ch>=''&&ch<=''){xx=(xx<<)+(xx<<)+ch-'';ch=getchar();}
return xx*ff;
}
long long READ(){
long long xx=,ff=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')ff=-;ch=getchar();}
while(ch>=''&&ch<=''){xx=(xx<<)+(xx<<)+ch-'';ch=getchar();}
return xx*ff;
}
long long N,q;
int Q;
inline long long can(long long a,long long b){//计算在连续区间内有几个元素是需要移动的(下标为偶数)
long long seq=(b-a+);
if(seq<=)
return ;
if(seq&){
if(b&)
return seq/;
else
return seq/+;
}
else
return seq/;
}
void dfs(long long now,long long L,long long R){//now:询问元素所在的位置 L:可能移动的连续区间左端点 R:右端点
long long t=R+can(L,R);
if(R<now)
dfs(now,R+,t);
else{
long long np=can(L,now-)+R+;//np:now移动后应该在的位置
if(np>N||(now&)){
printf("%I64d\n",now/+);
return;
}
dfs(np,now+,np);
}
}
int main(){
//freopen("in","r",stdin);
N=READ()*-,Q=read();
for(int i=;i<=Q;i++){
q=READ();
long long r=N/+;
dfs(q,,r);
}
return ;
}
codeforces 949B A Leapfrog in the Array的更多相关文章
- codeforces 949B :A Leapfrog in the Array 找规律
题意: 现在给你一个n,表示有2*n-1个方格,第奇数方格上会有一个数字 1-n按顺序放.第偶数个方格上是没有数字的.变动规则是排在最后一个位置的数字,移动到它前边最近的空位 . 直到数字之间没有空位 ...
- Codeforces 950D A Leapfrog in the Array (思维)
题目链接:A Leapfrog in the Array 题意:给出1-n的n个数,从小到大每隔一个位置放一个数.现在从大到小把数往前移动,每次把最右边的数移动最靠右边的空格处直到n个数都在前n个位置 ...
- CodeForces - 950D A Leapfrog in the Array 玄学题
题意:n个数1~n(n<=1e18)依次放在一个数组中,第i个数位置为2i-1,其它地方是空的.现在重复以下操作:将最右边的数放到离其左边最近的空的位置,直到所有数移到前一半的位置中.有q< ...
- Codeforces 950D A Leapfrog in the Array ( 思维 && 模拟 )
题意 : 给出 N 表示有标号 1~N 的 N 个数,然后从下标 1 开始将这 N 个数每隔一位放置一个,直到 N 个数被安排完,现在有一个操作就是每次将数列中最右边的数向离其左边最近的空缺处填上,一 ...
- Codeforces 221d D. Little Elephant and Array
二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...
- Codeforces 950 D. A Leapfrog in the Array
http://codeforces.com/contest/950/problem/D 前n/2个格子的奇数下标的数没有参与移动 候n/2个格子的奇数下标的数一定是一路移向偶数下标移 所以还原数的初始 ...
- Codeforces Round #181 (Div. 2) A. Array 构造
A. Array 题目连接: http://www.codeforces.com/contest/300/problem/A Description Vitaly has an array of n ...
- B. A Leapfrog in the Array
http://codeforces.com/problemset/problem/949/B Dima is a beginner programmer. During his working pro ...
- Codeforces Round #284 (Div. 1) C. Array and Operations 二分图最大匹配
题目链接: http://codeforces.com/problemset/problem/498/C C. Array and Operations time limit per test1 se ...
随机推荐
- 00HyperText Markup Language
HyperText Markup Language HTML超文本标记语言是一种用于创建网页的标准标记语言用于显示网页内容,HTML运行在浏览器上,由浏览器来解析,您可以使用 HTML 来建立自己的 ...
- POJ2152 Fire (树形DP)
题意:n个城市n-1条边 组成一棵树 在每个城市修建消防站会有一个花费costi 每个城市能防火当且仅当地图上距离他最近的消防站距离小于di 问如何修建消防站 使地图上所有的城市都有预防火灾的能力 ...
- 小程序wx:key = “{{*this}}”报错
解决方案:改为 wx:key = "*this"
- vue v-model 的注意点
在使用表单元素 input 的 v-model 指令时,碰到一个问题: 如上图,修改 input 的内容,以便随时显示:但显示时明显慢一步. <template> <div> ...
- LINUX-APT 软件工具 (Debian, Ubuntu 以及类似系统)
apt-get install package_name 安装/更新一个 deb 包 apt-cdrom install package_name 从光盘安装/更新一个 deb 包 apt-get u ...
- Eclipse 导出的jar包 , 使用后提示重复定义?
导出jar包时,一般会指定一个路径,导出的完整jar包就会自动放到那个指定路径里. 后来我发现那个指定路径的jar包比bin文件夹里面的jar包大,于是就用bin文件夹里面的jar包代替来试试,果然不 ...
- 【MongoDB】2、安装MongoDB 2.6.1 on Unbuntu 14.04(学习流水账)
http://blog.csdn.net/stationxp/article/details/26077439 计划: 装一个虚机,ubuntu吧,14.04 Trusty Tahr. 安装Mongo ...
- HDU 1042 大数计算
这道题一开始就采用将一万个解的表打好的话,虽然时间效率比较高,但是内存占用太大,就MLE 这里写好大数后,每次输入一个n,然后再老老实实一个个求阶层就好 java代码: /** * @(#)Main. ...
- [luoguP1156] 垃圾陷阱(DP)
传送门 先按照时间排序 f[i][j] 表示 前i个物品高度为j时所剩余的最大能量 显然每个物品有堆和吃两种选择 状态转移看代码 代码 #include <cstdio> #include ...
- 清北学堂模拟赛d1t2 火柴棒 (stick)
题目描述众所周知的是,火柴棒可以拼成各种各样的数字.具体可以看下图: 通过2根火柴棒可以拼出数字“1”,通过5根火柴棒可以拼出数字“2”,以此类推. 现在LYK拥有k根火柴棒,它想将这k根火柴棒恰好用 ...