码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ 2093: [Poi2010]Frog

时间:2016-12-23 23:06:40      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:esc   memory   line   des   har   for   class   space   cep   

Description

从一个点到达与他距离第 \(k\) 小的点,问从每个点跳 \(m\) 次到达那个点.

Sol

队列+倍增.

保持队列里的元素个数为 \(k\) ,从前往后扫不难发现左右端点都是单调的.

求跳 \(m\) 次就是倍增了,滚一下数组.

Code

/**************************************************************
    Problem: 2093
    User: BeiYu
    Language: C++
    Result: Accepted
    Time:7256 ms
    Memory:20832 kb
****************************************************************/
 
#include <bits/stdc++.h>
using namespace std;
 
typedef long long LL;
const int N = 1e6+50;
const int M = 65;
 
 
LL n,k,m,l,r,lm;
LL a[N],pow2[M];
int f[2][N],g[N];
 
inline LL in(LL x=0,char ch=getchar()) { while(ch>‘9‘ || ch<‘0‘) ch=getchar();
    while(ch>=‘0‘ && ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar();return x; }
int main() {
    n=in(),k=in(),m=in(),lm=log2(m)+1;
    for(int i=1;i<=n;i++) a[i]=in();
     
    l=1,r=k+1;
     
    for(int i=1;i<=n;i++) {
        while(r<n && a[r+1]-a[i] < a[i]-a[l]) r++,l++;
        f[0][i]=(a[r]-a[i] > a[i]-a[l] ? r : l);
    }
     
    pow2[0]=1;for(int i=1;i<=lm;i++) pow2[i]=pow2[i-1]<<1;
    if(m&1) for(int i=1;i<=n;i++) g[i]=f[0][i];
    else for(int i=1;i<=n;i++) g[i]=i;
    r=1;
    for(int j=1;j<=lm;j++) {
        for(int i=1;i<=n;i++) f[r][i]=f[r^1][f[r^1][i]];
        if(m&pow2[j]) for(int i=1;i<=n;i++) g[i]=f[r][g[i]];
        r^=1;
    }
    for(int i=1;i<=n;i++) printf("%d%c",g[i]," \n"[i==n]);
    return 0;
}

 

BZOJ 2093: [Poi2010]Frog

标签:esc   memory   line   des   har   for   class   space   cep   

原文地址:http://www.cnblogs.com/beiyuoi/p/6216141.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!