B. A Leapfrog in the Array
http://codeforces.com/problemset/problem/949/B
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].
思维题,找规律
1.n为奇数的话,可以用n-1偶数过渡过来(只需要把最后一个丢到最前面)
2.询问的k为奇数的话,直接返回(k+1)/2,因为位置是不改变的
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 2147483647
const ll INF = 0x3f3f3f3f3f3f3f3fll;
#define ri register int
template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
template <class T> inline T min(T a, T b, T c, T d) {
return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d) {
return max(max(a, b), max(c, d));
}
#define scanf1(x) scanf("%d", &x)
#define scanf2(x, y) scanf("%d%d", &x, &y)
#define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X)
#define pi acos(-1)
#define me(x, y) memset(x, y, sizeof(x));
#define For(i, a, b) for (int i = a; i <= b; i++)
#define FFor(i, a, b) for (int i = a; i >= b; i--)
#define bug printf("***********\n");
#define mp make_pair
#define pb push_back
const int maxn = ;
// name*******************************
ll T;
ll k, n;
ll ans;
// function******************************
ll solve(ll n, ll k) {
if (n % == ) {
if (k % ) {
return (k + ) / ;
} else {
return solve(n / , k / ) + n / ;
}
} else {
if (k % ) {
return (k + ) / ;
} else {
if (k == ) {
return solve(n - , n - ) + ;
} else {
return solve(n - , k - ) + ;
}
}
}
} //***************************************
int main() {
// ios::sync_with_stdio(0);
// cin.tie(0);
// freopen("test.txt", "r", stdin);
// freopen("outout.txt","w",stdout);
cin >> n >> T;
while (T--) {
ans = ;
ll k;
cin >> k;
cout <<solve(n, k) << endl;
} return ;
}
B. A Leapfrog in the Array的更多相关文章
- Codeforces 950D A Leapfrog in the Array (思维)
题目链接:A Leapfrog in the Array 题意:给出1-n的n个数,从小到大每隔一个位置放一个数.现在从大到小把数往前移动,每次把最右边的数移动最靠右边的空格处直到n个数都在前n个位置 ...
- codeforces 949B A Leapfrog in the Array
B. A Leapfrog in the Array time limit per test 2 seconds memory limit per test 512 megabytes input s ...
- Codeforces 950 D. A Leapfrog in the Array
http://codeforces.com/contest/950/problem/D 前n/2个格子的奇数下标的数没有参与移动 候n/2个格子的奇数下标的数一定是一路移向偶数下标移 所以还原数的初始 ...
- CodeForces - 950D A Leapfrog in the Array 玄学题
题意:n个数1~n(n<=1e18)依次放在一个数组中,第i个数位置为2i-1,其它地方是空的.现在重复以下操作:将最右边的数放到离其左边最近的空的位置,直到所有数移到前一半的位置中.有q< ...
- codeforce469DIV2——D. A Leapfrog in the Array
题意: 给出1<=n<=10^18和1<=q<=200000,有一个长度为2*n-1的数组,初始时单数位置存(i+1)/2,双数位置是空的.每次找出最右边的一个数将它跳到离它最 ...
- cf950d A Leapfrog in the Array
考虑在位置 \(p\) 的青蛙. 如果 \(p\) 是奇数,答案显然是 \((p+1)/2\). 否则,由于未跳时 \(p\) 左边有 \(p/2\) 只,则 \(p\) 右边有 \(n-p/2\) ...
- CF949B A Leapfrog in the Array
思路: 最终的时候,对于位置p,若p是奇数,则该位置的元素是(p + 1) / 2:若p是偶数,需要从p开始不断地迭代寻找上一次跳跃所处的位置(p = p + n - p / 2),直到p是奇数为止. ...
- CF949B A Leapfrog in the Array 思维题,推理
题意: Dima是一名初级程序员. 在他的工作中,他经常不断地重复以下操作:从数组中删除每个第二个元素. 有一天,他对这个问题的解决方案感到厌倦,他提出了以下华丽的算法. 假设有一长度为2n的数组,最 ...
- Codeforces 950D A Leapfrog in the Array ( 思维 && 模拟 )
题意 : 给出 N 表示有标号 1~N 的 N 个数,然后从下标 1 开始将这 N 个数每隔一位放置一个,直到 N 个数被安排完,现在有一个操作就是每次将数列中最右边的数向离其左边最近的空缺处填上,一 ...
随机推荐
- AGC009:Eternal Average
传送门 好神啊 直接考虑一棵 \(n+m\) 个叶子的 \(k\) 叉树,根结点权值为 \(\sum_{i\in m}(\frac{1}{k})^{deep_i}\) 对于一个 \(deep\) 的序 ...
- 在element-ui的el-tree组件中用render函数生成el-button
本文主要介绍怎么在el-tree组件中通过render函数来el-button. 这是element-ui中el-tree树: 这是需要实现的效果: tree.vue文件中,具体实现的代码如下: &l ...
- CSS属性之border
css的border属性相信大家都不陌生了,就是给元素加个边框嘛,在不同的盒模型下,会给元素的宽高带来怎样的影响,相信大家也都很熟悉了,这里就不再赘述,只说说大家平时没有怎么留意的东西. 0.bord ...
- python之系统命令
subprocess执行系统命令 call:执行命令,返回状态码 #!/usr/bin/env python # _*_ coding:utf-8 _*_ __Author__ = 'KongZhaG ...
- python入门の缩进魔术
idx=1 sum=0 while idx<=100: sum=idx+sum idx=idx+1 print('sum 100 =', sum) ======================= ...
- [SQL Server]SQL行转列
SELECT * FROM (select ActionTargetType+actiontype as TypeResult, COUNT(RowGuid) as Number from BanJi ...
- mysql优化——explain详解
MySQL的EXPLAIN命令用于SQL语句的查询执行计划(QEP).这条命令的输出结果能够让我们了解MySQL 优化器是如何执行SQL 语句的.这条命令并没有提供任何调整建议,但它能够提供重要的信息 ...
- ELT探索之旅2 kettle配置
java环境变量配置: path增加 ;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; 双击spoon.bat即可
- [翻译] UIColor-uiGradientsAdditions
UIColor-uiGradientsAdditions https://github.com/kaiinui/UIColor-uiGradientsAdditions Beautiful color ...
- UNIX高级环境编程(9)进程控制(Process Control)- fork,vfork,僵尸进程,wait和waitpid
本章包含内容有: 创建新进程 程序执行(program execution) 进程终止(process termination) 进程的各种ID 1 进程标识符(Process Identifie ...