codeforces 949B A Leapfrog in the Array

本文介绍了一个独特的数组操作算法,该算法由程序员Dima提出,旨在通过特定规则将数组中的元素移动到新的位置。文章详细解释了算法的工作原理,并提供了一个递归模拟的解决方案来预测指定位置上最终出现的数字。
B. A Leapfrog in the Array
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
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.

Input

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.

Output

For each of q queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes.

Examples
input
Copy
4 3
2
3
4
output
3
2
4
input
Copy
13 4
10
5
4
8
output
13
3
8
9
Note

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的顺序看题目中的图,递归模拟,注意细节。

 

 1 /*
 2 Welcome Hacking
 3 Wish You High Rating
 4 */
 5 #include<iostream>
 6 #include<cstdio>
 7 #include<cstring>
 8 #include<ctime>
 9 #include<cstdlib>
10 #include<algorithm>
11 #include<cmath>
12 #include<string>
13 using namespace std;
14 int read(){
15     int xx=0,ff=1;char ch=getchar();
16     while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
17     while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
18     return xx*ff;
19 }
20 long long READ(){
21     long long xx=0,ff=1;char ch=getchar();
22     while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
23     while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
24     return xx*ff;
25 }
26 long long N,q;
27 int Q;
28 inline long long can(long long a,long long b){//计算在连续区间内有几个元素是需要移动的(下标为偶数)
29     long long seq=(b-a+1);
30     if(seq<=0)
31         return 0;
32     if(seq&1){
33         if(b&1)
34             return seq/2;
35         else
36             return seq/2+1;
37     }
38     else
39         return seq/2;
40 }
41 void dfs(long long now,long long L,long long R){//now:询问元素所在的位置 L:可能移动的连续区间左端点 R:右端点
42     long long t=R+can(L,R);
43     if(R<now)
44         dfs(now,R+1,t);
45     else{
46         long long np=can(L,now-1)+R+1;//np:now移动后应该在的位置
47         if(np>N||(now&1)){
48             printf("%I64d\n",now/2+1);
49             return;
50         }
51         dfs(np,now+1,np);
52     }
53 }
54 int main(){
55     //freopen("in","r",stdin);
56     N=READ()*2-1,Q=read();
57     for(int i=1;i<=Q;i++){
58         q=READ();
59         long long r=N/2+1;
60         dfs(q,1,r);
61     }
62     return 0;
63 }
View Code

 

 

 

 

转载于:https://www.cnblogs.com/lzhAFO/p/8536647.html

### 关于 Codeforces 1853B 的题解实现 尽管当前未提供关于 Codeforces 1853B 的具体引用内容,但可以根据常见的竞赛编程问题模式以及相关算法知识来推测可能的解决方案。 #### 题目概述 通常情况下,Codeforces B 类题目涉及基础数据结构或简单算法的应用。假设该题目要求处理某种数组操作或者字符串匹配,则可以采用如下方法解决: #### 解决方案分析 如果题目涉及到数组查询或修改操作,一种常见的方式是利用前缀和技巧优化时间复杂度[^3]。例如,对于区间求和问题,可以通过预计算前缀和数组快速得到任意区间的总和。 以下是基于上述假设的一个 Python 实现示例: ```python def solve_1853B(): import sys input = sys.stdin.read data = input().split() n, q = map(int, data[0].split()) # 数组长度和询问次数 array = list(map(int, data[1].split())) # 初始数组 prefix_sum = [0] * (n + 1) for i in range(1, n + 1): prefix_sum[i] = prefix_sum[i - 1] + array[i - 1] results = [] for _ in range(q): l, r = map(int, data[2:].pop(0).split()) current_sum = prefix_sum[r] - prefix_sum[l - 1] results.append(current_sum % (10**9 + 7)) return results print(*solve_1853B(), sep='\n') ``` 此代码片段展示了如何通过构建 `prefix_sum` 来高效响应多次区间求和请求,并对结果取模 \(10^9+7\) 输出[^4]。 #### 进一步扩展思考 当面对更复杂的约束条件时,动态规划或其他高级技术可能会被引入到解答之中。然而,在没有确切了解本题细节之前,以上仅作为通用策略分享给用户参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值