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 ...
随机推荐
- 取得数据库中数据 查询条件where使用规则
string where = string.Format("DnX < {0} and DnD > {0} and Types = '{1}' and Type1 = '{2}' ...
- ThinkPHP---ue编辑器
[一]简介 (1)介绍 一款百度开发的JS插件 ue编辑器,全称Ueditor(翻译为你的编辑器),百度开发的在线编辑器,是一款在线编辑器软件,在线编辑器又称为“富文本编辑器”. 国外也有一款类似的编 ...
- 牛客多校Round 4
Soved:3 rank:133 A.Ternay String 欧拉降幂一下 但是反复求phi会超时 但mod是同一个就可以记忆化一下 #include <bits/stdc++.h> ...
- jsp中的basePath,获取应用的路径
1 2 3 4 5 String path = request.getContextPath(); String basePath = request.getScheme()+": ...
- DTD 文件的引入
MyBatis 有两种配置文件:核心配置文件(mybatis- config.xml)和 SQL 映射文件(mapper.xml).这两种配置文件都需要手动引入各自的 DTD 文件(mybatis-3 ...
- 登录deepin 15.9后不显示任务栏,无法操作
一直觉得在Linux下编程很酷,所以决定装个Deepin试试,安装很顺利,然后搭建了开发环境,写了一个简单程序,觉得挺不错的. 哪知第二天一开机,登录后找不到任务栏了,做不了啥操作,走接傻眼了,直觉以 ...
- CCF201612-1 中间数 java(100分)
试题编号: 201612-1 试题名称: 中间数 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 在一个整数序列a1, a2, …, an中,如果存在某个数,大于它的整数数量等 ...
- 【3】数据筛选3 - BeautifulSoup4
#目录 1. 开发前准备 2. 不同解析器对比 3. BeautifulSoup4 初始化和节点对象的认识 4. BS4 案例操作:初始化对象文档 5. 节点查 ...
- Sublime text如何设置快捷键让编写的HTML文件在浏览器预览?
STEP 1 Tools->Build System->New Build System STEP 2<img src="https://pic3.zhimg.com/ ...
- STL源码剖析 学习笔记 MiniSTL
https://github.com/joeyleeeeeee97 目录: 第二章 空间适配器 第三章 迭代器 第四章 序列式容器(vector,list,deque,stack,heap,prior ...