Evanyou Blog 彩带

P3509 [POI2010]ZAB-Frog

 

题目描述

 

On the bed of one particularly long and straight Byteotian brook there lie  rocks jutting above the water level.

 

Their distances from the brook's spring are  respectively.

 

A small frog sitting on one of these is about to begin its leaping training.

 

Each time the frog leaps to the rock that is the -th closest to the one it is sitting on.

 

Specifically, if the frog is sitting on the rock at position , then it will leap onto such  that:

 

 and  If  is not unique, then the frog chooses among them the rock that is closest to the spring.

 

On which rock the frog will be sitting after  leaps depending on the rock is started from?

 

数轴上有n个点,有一个青蛙在这些点上跳;

 

规则是每次向距当前点第k小的点跳,如果有相同距离则向下标较小的跳;

 

求从每个点出发跳了m次后在哪里;

 

输入输出格式

输入格式:

 

 

The first line of the standard input holds three integers,  and  (), separated by single spaces, that denote respectively: the number of rocks, the parameter , and the number of intended leaps.

 

The second line holds  integers  (), separated by single spaces, that denote the positions of successive rocks on the bed of the brook.

 

 

输出格式:

 

 

Your program should print a single line on the standard output, with  integers  from the interval in it, separated by single spaces.

 

The number  denotes the number of the rock that the frog ends on after making  leaps starting from the rock no.  (in the input order).

 

 

 

输入输出样例

 

输入样例#1: 
5 2 4
1 2 4 7 10
输出样例#1: 
1 1 3 1 1

 

 

Solution:

  本题贼有意思,尺取法+倍增。

  首先考虑预处理出每个位置的第$k$近的数位置,针对数据$n\leq 10^6$,很显然只能线性或者$n\log n$预处理。

  题目中很明确的给出了序列严格单调不下降,那么对于$i$位置的数,不难想到构造一个长度为$k$的区间$[l,r],r-l+1=k$使得$i\in[l,r]$(其实肯定在区间里),由于单调性,于是答案肯定是$a[i]-a[l],a[r]-a[i]$中较大的一个数的位置。于是不难想到用尺取法去求每个位置所对应的第$k$近的数的位置,这样就是$O(n)$的预处理。

  然后再考虑如何去求$m$次后的位置,最暴力的方法无疑是$1\rightarrow m$扫一遍,每次对每个数都移动到它的下个位置,这样复杂度为$O(nm)$显然爆了。

  那么优化的方法就是倍增了,我们用类似于快速幂的方法,$m$可以转为$2^{p_1}+2^{p_2}+…2^{p_k}$,先移动到$2^1$次的位置,再移到$2^2$次的位置…若二进制的第$p_i$位为1则对答案先移动前面求出的$p_{i+1}$次(可以类比下快速幂),这样就优化到了$O(n\log m)$了。

代码:

 

#include<bits/stdc++.h>
#define il inline
#define ll long long
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=1000005;
int n,k,ans[N],f[N],g[N];
ll a[N],m;

il ll gi(){
    ll a=0;char x=getchar();
    while(x<'0'||x>'9')x=getchar();
    while(x>='0'&&x<='9')a=(a<<3)+(a<<1)+x-48,x=getchar();
    return a;
}

int main(){
    n=gi(),k=gi(),m=gi();
    For(i,1,n)  a[i]=gi();
    int l=1,r=k+1;f[1]=r;
    For(i,2,n){
        while(r<n&&a[i]-a[l]>a[r+1]-a[i]) l++,r++;
        if(a[i]-a[l]>=a[r]-a[i]) f[i]=l;
        else f[i]=r;
    }
    For(i,1,n) ans[i]=i;
    while(m){
        if(m&1) For(i,1,n) ans[i]=f[ans[i]];
        For(i,1,n) g[i]=f[f[i]];
        For(i,1,n) f[i]=g[i];
        m>>=1;
    }
    For(i,1,n) printf("%d ",ans[i]);
    return 0;
}

 

posted @ 2018-07-17 19:41  five20  阅读(358)  评论(0编辑  收藏  举报
Live2D