CF - 一直交换元素的规律
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].
题意 : 题意看这四张图就可以了,还是很明确的,每次将最末位的元素移动到前面的一个空位置上。
思路分析 : 暴力写一下,发现它的移动是有规律的,奇数位的数是不发生变动的

代码示例 :
#define ll long long
const int maxn = 1e6+5;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f; int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
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;
}
CF - 一直交换元素的规律的更多相关文章
- [LeetCode] “全排列”问题系列(一) - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题
一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer上的一道例题入手,小谈一下这种类型题目的解法. 二.上手 最典型的permutation题目是这样的 ...
- “全排列”问题系列(一)[LeetCode] - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题
转:http://www.cnblogs.com/felixfang/p/3705754.html 一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer ...
- jquery插件——点击交换元素位置(带动画效果)
一.需求的诞生 在我们的网页或者web应用中,想要对列表中的元素进行位置调整(或者说排序)是一个常见的需求.实现方式大概就以下两种,一种是带有类似“上移”.“下移”的按钮,点击可与相邻元素交换位置,另 ...
- iOS Swift 数组 交换元素的两种方法
swap(&arr[fromIndexPath.row], &arr[to.row]) (arr[fromIndexPath.row],arr[to.row]) = (arr[to.r ...
- js 实现数组元素交换位置
/** * 数组元素交换位置 * @param {array} arr 数组 * @param {number} index1 添加项目的位置 * @param {number} index2 删除项 ...
- 【codeforces】【比赛题解】#849 CF Round #431 (Div.2)
cf的比赛越来越有难度了……至少我做起来是这样. 先看看题目吧:点我. 这次比赛是北京时间21:35开始的,算是比较良心. [A]奇数与结束 "奇数从哪里开始,又在哪里结束?梦想从何处起航, ...
- 如何交换两个等长整形数组使其数组和的差最小(C和java实现)
1. 问题描述: 有两个数组a,b,大小都为n,数组元素的值任意整形数,无序: 要求:通过交换a,b中的元素,使[数组a元素的和]与[数组b元素的和]之间的差最小. 2. 求解思路: 当前数组a和数组 ...
- jQuery使用之(一)标记元素属性
jQuery使用主要介绍jQuery如何控制页面,包含元素的属性.css样式风格.DOM模型.表单元素和事件处理等. 标记元素的属性 html中每一个标记都具有一些属性,他们这个标记在页面中呈现各种状 ...
- 查询无序列表中第K小元素
当需要在无需列表中寻找第k小的元素时,一个显然的方法是将所有数据进行排序,然后检索k个元素.这种方法的运行时间为O(n log(n)). 无序列表调用分区函数将自身分解成两个子表,其长度为i和n-i. ...
随机推荐
- centos7 安装R和RstudioServer版
参考: http://www.cnblogs.com/inspursu/p/4275701.html http://blog.csdn.net/u010022051/article/details/5 ...
- RabbitMQ-事务和Confirm消息确认
,如果要保证消息的可靠性,需要对消息进行持久化处理,然而消息持久化除了需要代码的设置之外,还有一个重要步骤是至关重要的,那就是保证你的消息顺利进入Broker(代理服务器),如图所示: 正常情况下,如 ...
- C# 将 Begin 和 End 异步方法转 task 异步
在 .NET Framework 有两个不同的异步方法,一个是 Asynchronous Programming Model (APM) 另一个是 Task-based asynchronous pa ...
- Linux 内核 标准 PCI 配置寄存器
一些 PCI 配置寄存器是要求的, 一些是可选的. 每个 PCI 设备必须包含有意 义的值在被要求的寄存器中, 而可选寄存器的内容依赖外设的实际功能. 可选的字段不被 使用, 除非被要求的字段的内容指 ...
- js实现bind
Function.prototype.bind=function(ctx,...lastArgs){ let self=this return (...laterArgs)=>self.appl ...
- Resharper 去掉注释拼写
最近在 Resharper 的 2018.2.1 的版本,提供了单词拼写功能,如果自己写错了单词,可以在 Resharper 提示 Resharper 的拼写 在 Resharper 的 2018.2 ...
- Cookie的使用、Cookie详解、HTTP cookies 详解、获取cookie的方法、客户端获取Cookie、深入解析cookie
Cookie是指某些网站为了辨别用户身份.进行session跟踪而存储在用户本地终端上的数据(通常经过加密),比如说有些网站需要登录才能访问某个页面,在登录之前,你想抓取某个页面内容是不允许的.那么我 ...
- Github Pages 无法调用 node_modules 文件夹的解决方案
今天写一个demo,用npm安装的前端库,然后想在github的pages上展示出来 发布到github后,发现node_modules文件夹下的js无法调用 google解决方案:新增一个名字为.n ...
- HDU1166 敌兵布阵 BZOJ1012 最大数[树状数组]
一.前置知识-树状数组 树状数组(binary indexed tree)是一种简洁的代码量很小的数据结构,能够高效的处理前缀区间上的问题.在很多情况下能写树状数组解决的就不用码半天线段树了. 树状数 ...
- pyspider 安装使用过程的一些坑
1.没有正确安装对应版本的pycurl 原因分析: PyCurl 安装错误,需要安装 PyCurl 库(PyCurl 是一个Python接口,是多协议文件传输库的 libcurl.类似于urllib ...