codeforce469DIV2——D. A Leapfrog in the Array

题意:

给出1<=n<=10^18和1<=q<=200000,有一个长度为2*n-1的数组,初始时单数位置存(i+1)/2,双数位置是空的。每次找出最右边的一个数将它跳到离它最近的左边的空格,反复操作直到前n个数被充满 。q个询问,每个询问给出一个数x<=n,输出最终的序列x位置的数是什么。

分析:

一看数据规模和这个题就大概想到是数学题,然后试着找规律推式子。按照蒟蒻我的惯例这种题先写暴力找规律。然后发现,每次跳,第一次跳跳一个,再一次跳两个,一直到跳到n-1个。 那么从左往右,第i个位置f(i)=f(n+i/2),当i%2==1时,f(i)=(i+1)/2。我们发现那个函数的复杂度很小具体我不太会算大概Logn?

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 
 6 using namespace std;
 7 typedef unsigned long long ull;
 8 
 9 ull n,q;
10 
11 ull f(ull pos){
12     if(pos%2){
13         return (pos+1)/2;
14     }
15     ull p=n-pos/2;
16     return f(pos+p);
17 }
18 int main(){
19     cin>>n>>q;
20     ull x;
21     for(int i=1;i<=q;i++){
22         cin>>x;
23         cout<<f(x)<<endl;
24     }
25 return 0;
26 }
View Code
原文地址:https://www.cnblogs.com/LQLlulu/p/8784743.html