Codeforces Round #469 D. A Leapfrog in the Array【思维】

题目链接

题意

一个序列初始为 1 _ 2 _ 3 _ . . . _ n − 1 _ n 1\_2\_3\_...\_n-1\_n 1_2_3_..._n1_n(_为空),每次操作会把最后一个数往左移到第一个空,操作持续到无法移动为止(即1~n把前面全占满,没有空了), q q q 次询问位置 P P P 的值 ( 1 ≤ P ≤ n 1\le P \le n 1Pn

操作如下图所示
在这里插入图片描述

题解

感性的理解一下,操作到最后,1~n 奇数位上的数是没有移动过的,因为
处理到 n 2 \cfrac{n}{2} 2n 时,序列长这样: 1 _ 2 _ 3 _ . . _ n 2 a b c d . . _ _ _ _ _ 1\_2\_3\_..\_\cfrac{n}{2}abcd..\_\_\_\_\_ 1_2_3_.._2nabcd.._____,后一半的数会连在一起,然后把前面的空全填上

同理对于后一半的数,处理到 n 4 \cfrac{n}{4} 4n时,奇数位置也没变,对于后一半的后一半也是。。。。

所以就一直往后找,把 P 加回去直到它是奇数位置为止

#include<iostream>
#include<sstream>
#include<string>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include <utility>
#include<list>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<time.h>
#include<random>
using namespace std;
#include<ext/pb_ds/priority_queue.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
#include<ext/rope>
using namespace __gnu_cxx;

#define int long long
#define PI acos(-1.0)
#define eps 1e-9
#define lowbit(a) ((a)&-(a))
#define mid ((l+r)>>1)
#define mem(x,y) memset(x,y,sizeof x)

const int mod = 1e9+7;
int qpow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=(ans*a)%mod;
		a=(a*a)%mod;
		b>>=1;
	}
	return ans;
}
const int INF = 0x3f3f3f3f;
const int N = 1e6+10;

#define endl '\n'
signed main(){
	//freopen("C:\\Users\\egoist\\Desktop\\in.txt","r",stdin);
	//freopen("C:\\Users\\egoist\\Desktop\\out.txt","w",stdout);
	std::ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int n,q; cin>>n>>q;
	while(q--){
		int p; cin>>p;
		while(!(p&1))p+=(n-(p>>1));
		cout<<((p>>1)+1)<<endl;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值