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 个数被安排完,现在有一个操作就是每次将数列中最右边的数向离其左边最近的空缺处填上,一 ...
随机推荐
- springboot中使用druid和监控配置
如果想要监控自己的项目的访问情况及查看配置信息,druid是一个很好的选择,可能你会问druid是什么?有什么用?优点是什么? Druid简介 Druid是阿里巴巴开源的数据库连接池,号称是Java语 ...
- jQuery基础(动画篇 animate,显示隐藏,淡入淡出,下拉切换)
1.jQuery中隐藏元素的hide方法 让页面上的元素不可见,一般可以通过设置css的display为none属性.但是通过css直接修改是静态的布局,如果在代码执行的时候,一般是通过js控制元 ...
- Json反序列化为动态类型(dynamic)
方法(依赖于Newtonsoft.Json): /// <summary> /// 反序列化json字符串 /// </summary> /// <typeparam n ...
- 无法在类...中找到资源".bmp"
在WinForm中写的一个程序,在项目中添加了一个bmp图片,然后 public void SetSubType(int SubType) { m_subType = SubType; switch ...
- CentOS7系列--1.6CentOS7配置sudo
CentOS7配置sudo 如果一些用户共享权限,配置sudo是为了分离用户的职责 1. 将root 的权限传递给所有用户 [root@centos7 ~]# visudo 添加下面的内容到最后一行, ...
- Git『Everything up-to-date』问题解决
今天提交代码的时候遇到了一个小问题,这里解决了记下小记. 提交代码遇到『Everything up-to-date』 上网查了下,发现中文大多答非所问,少数能解决的并没有阐述原理,所以我写这篇文章记录 ...
- Dlink DIR-823G 漏洞挖掘过程
前言 本文由 本人 首发于 先知安全技术社区: https://xz.aliyun.com/u/5274 初步分析 首先下载固件 https://gitee.com/hac425/blog_data/ ...
- 3The superclass “javax.servlet.http.HttpServlet" was not found on the Java Build Path 之一
另外一篇短文里还有第三种解决方案,查看请点击这里 1.异常信息 创建maven web项目时,出现 The superclass “javax.servlet.http.HttpServlet&quo ...
- 关于C#线程,线程池和并行运算的简单使用和对比
转自:https://www.cnblogs.com/jeffwongishandsome/archive/2010/11/12/1876137.html 先大概看一下控制台应用程序的Main方法的主 ...
- 从零开始学习Docker
由于项目中可能用到docker容器,在此记录一下我的学习过程 1,docker的安装,wget -qO- https://get.docker.com/ | sh,查看docker是否安装成功: 此处 ...