题目描述

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=;
int n,k,ans[N],f[N],g[N];
ll a[N],m; il ll gi(){
ll a=;char x=getchar();
while(x<''||x>'')x=getchar();
while(x>=''&&x<='')a=(a<<)+(a<<)+x-,x=getchar();
return a;
} int main(){
n=gi(),k=gi(),m=gi();
For(i,,n) a[i]=gi();
int l=,r=k+;f[]=r;
For(i,,n){
while(r<n&&a[i]-a[l]>a[r+]-a[i]) l++,r++;
if(a[i]-a[l]>=a[r]-a[i]) f[i]=l;
else f[i]=r;
}
For(i,,n) ans[i]=i;
while(m){
if(m&) For(i,,n) ans[i]=f[ans[i]];
For(i,,n) g[i]=f[f[i]];
For(i,,n) f[i]=g[i];
m>>=;
}
For(i,,n) printf("%d ",ans[i]);
return ;
}

P3509 [POI2010]ZAB-Frog的更多相关文章

  1. [洛谷P3509][POI2010]ZAB-Frog

    题目大意:有$n$个点,每个点有一个距离(从小到大给出),从第$i$个点跳一次,会跳到距离第$i$个点第$k$远的点上(若有两个点都是第$k$远,就跳到编号小的上).问对于从每个点开始跳,跳$m$次, ...

  2. bzoj2093: [Poi2010]Frog(单调队列,倍增)

    2093: [Poi2010]Frog Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 568  Solved: 186[Submit][Status] ...

  3. BZOJ 2093: [Poi2010]Frog

    Description 从一个点到达与他距离第 \(k\) 小的点,问从每个点跳 \(m\) 次到达那个点. Sol 队列+倍增. 保持队列里的元素个数为 \(k\) ,从前往后扫不难发现左右端点都是 ...

  4. BZOJ2093 : [Poi2010]Frog

    从左往右维护两个指针l,r表示离i最近的k个点的区间,预处理出每个点出发的后继,然后倍增. #include<cstdio> typedef long long ll; const int ...

  5. [POI2010]Frog

    题目大意: 一个数轴上有n个点,现在你要在这些点上跳. 每次跳的时候你只能跳到离这个点第k近的点上,而且要连续跳m次. 问从每一个点出发,最后分别会在哪一个点结束. 思路: 首先可以维护一个大小为k+ ...

  6. bzoj 2093 [Poi2010]Frog——滑动窗口

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2093 找第k近的可以用一个含k个元素的滑动窗口来实现. 卡空间也还行,但卡时间.不要预处理倍 ...

  7. 洛谷P3509 Frog

    题目 首先分析数据范围发现m很大,所以线性做法肯定不行,因此考虑倍增,即预处理出每个点跳1次后的位置.然后只用两个数组类似于快速幂,推出每个点跳m次后的位置. 预处理离每个点第k小的点,可以用长度为k ...

  8. POI2010题解

    POI2010题解 我也不知道我为什么就开始刷POI了 有些题目咕掉了所以不完整(我都不知道POI到底有多少题) [BZOJ2079][Poi2010]Guilds (貌似bz跟洛谷上的不是一个题?) ...

  9. 分布式系统理论进阶 - Raft、Zab

    引言 <分布式系统理论进阶 - Paxos>介绍了一致性协议Paxos,今天我们来学习另外两个常见的一致性协议——Raft和Zab.通过与Paxos对比,了解Raft和Zab的核心思想.加 ...

随机推荐

  1. 【Win10分区教程】

    Win10怎么分区?如何为Win10硬盘分区? 注:本教程适用于Win7.Win8.Win8.1和Win10系 到了Windows10时代,TB级硬盘已经很普及了,那么在Win10系统下如何为这些大容 ...

  2. Mysql升级过程的问题

    升级安装5.6版本mysql linux环境下的yum默认mysql版本是5.1的,由于项目需要保存表情等4个字节的数据,版本受限,需要升级到5.6版本支持utf8mb4格式的编码. 升级过程大概就是 ...

  3. 我所用过的nginx的功能

    前言 当我们提起集群时,一般所用的插件就是nginx.nginx功能如今越来越完善.第三方模块也多如牛毛,在此,总结一下不牵扯第三方模块所具有的功能. 基本功能 反向代理 负载均衡 HTTP服务器(动 ...

  4. STL——vector和list

    vector和list为STL中的顺序容器,顺序容器会依次维护第一个到最后一个元素,在顺序容器上,我们主要的操作就是迭代. 头文件: #include<vector> #include&l ...

  5. Go语言使用百度翻译api

    Go语言使用百度翻译api 之前做过一个使用百度翻译api的工具,这个工具用于用户的自动翻译功能,是使用C#调用百度翻译api接口,既然在学习Go语言,那必然也是要使用Go来玩耍一番.这里我是这么安排 ...

  6. R语言绘图:箱线图

    使用ggplot2绘制箱线图 ######*****绘制箱线图代码*****####### data1$学区房 <- factor(data1$school, levels = 0:1, lab ...

  7. ABAP CDS ON HANA-(10)項目結合して一つ項目として表示

    Numeric Functions ABS(arg)  CEIL(arg) DIV(arg1, arg2) DIVISION(arg1, arg2, dec) FLOOR(arg) MOD(arg1, ...

  8. windows 系统禁止使用 U 盘的方法

    windows 系统禁止使用 U 盘的方法 最简单的办法: 注册表 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentCntrolSet\Services\USBSTOR] 将名为 ...

  9. malloc分配失败的两个现象

    在实际代码中,malloc的反复分配释放,可能会导致某一次malloc分配失败,虽然上一次调用malloc分配成功(然后释放),下一次在相同地方调用malloc分配可能会失败,疑问在于,既然上一次分配 ...

  10. ActiveMQ测试实例

    ActiveMQ的安装与启动 1 下载ActiveMQ:http://activemq.apache.org/download.html 2 下载后解压到任意文件夹,解压后文件夹内的目录为: 3 进入 ...