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).
输入输出样例
5 2 4
1 2 4 7 10
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的更多相关文章
- [洛谷P3509][POI2010]ZAB-Frog
题目大意:有$n$个点,每个点有一个距离(从小到大给出),从第$i$个点跳一次,会跳到距离第$i$个点第$k$远的点上(若有两个点都是第$k$远,就跳到编号小的上).问对于从每个点开始跳,跳$m$次, ...
- bzoj2093: [Poi2010]Frog(单调队列,倍增)
2093: [Poi2010]Frog Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 568 Solved: 186[Submit][Status] ...
- BZOJ 2093: [Poi2010]Frog
Description 从一个点到达与他距离第 \(k\) 小的点,问从每个点跳 \(m\) 次到达那个点. Sol 队列+倍增. 保持队列里的元素个数为 \(k\) ,从前往后扫不难发现左右端点都是 ...
- BZOJ2093 : [Poi2010]Frog
从左往右维护两个指针l,r表示离i最近的k个点的区间,预处理出每个点出发的后继,然后倍增. #include<cstdio> typedef long long ll; const int ...
- [POI2010]Frog
题目大意: 一个数轴上有n个点,现在你要在这些点上跳. 每次跳的时候你只能跳到离这个点第k近的点上,而且要连续跳m次. 问从每一个点出发,最后分别会在哪一个点结束. 思路: 首先可以维护一个大小为k+ ...
- bzoj 2093 [Poi2010]Frog——滑动窗口
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2093 找第k近的可以用一个含k个元素的滑动窗口来实现. 卡空间也还行,但卡时间.不要预处理倍 ...
- 洛谷P3509 Frog
题目 首先分析数据范围发现m很大,所以线性做法肯定不行,因此考虑倍增,即预处理出每个点跳1次后的位置.然后只用两个数组类似于快速幂,推出每个点跳m次后的位置. 预处理离每个点第k小的点,可以用长度为k ...
- POI2010题解
POI2010题解 我也不知道我为什么就开始刷POI了 有些题目咕掉了所以不完整(我都不知道POI到底有多少题) [BZOJ2079][Poi2010]Guilds (貌似bz跟洛谷上的不是一个题?) ...
- 分布式系统理论进阶 - Raft、Zab
引言 <分布式系统理论进阶 - Paxos>介绍了一致性协议Paxos,今天我们来学习另外两个常见的一致性协议——Raft和Zab.通过与Paxos对比,了解Raft和Zab的核心思想.加 ...
随机推荐
- 【TCP/IP实现磁盘资源的分享-----ISCSI(互联网最小应用程序接口)】
Iscsi server: 首先把多块磁盘合并为RAID5,便于后期iscis client访问以及服务端的管理 安装 targted服务端包,以及targtedcli创建iscsi TCP/IP共享 ...
- JavaScript -- 继承与原型链
JavaScript对象有一个指向一个原型对象的链,当试图访问一个对象的属性的时候,他不仅仅会在该对象上面搜寻,还会搜寻该对象的原型,以及对象的原型的原型,依次层层搜索,直到找到名字匹配的属性或者到达 ...
- I/O流、文件操作
1)操作文件 Path和Files是在JavaSE7中新添加进来的类,它们封装了在用户机器上处理文件系统所需的所有功能.Path表示的一个目录名序列,其后还可以跟着一个文件名.路径中的第一个参数可以是 ...
- python学习之字符串常用方法和格式化字符串
Python中的字符串同样适用标准的序列操作(索引,分片,乘法,成员判断,求长度,取最小值和最大值),但因为字符串是不可变的,因此字符串不支持分片赋值. s='http://www.baidu.com ...
- "Mon Dec 31 00:00:00 CST 2012" java日期装换 "yyyy-MM-dd"
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import ja ...
- linux io 学习笔记(01)---锁,信号量
1.采用信号量访问:当有段临界代码,需要保证排他的访问一个资源. 2.sudo dmesg -c 消除dmesg缓冲 3.互斥锁:代表的是一种锁资源,互斥锁的工作原理是:保证对共享资源操作的原子性 ...
- kylin实战系列(一)
kylin实战系列(一) 把之前kylin的实践小结一下,以备以后查看.
- bzoj 一些题目汇总
2140: 稳定婚姻 /* 求联通分量. */ #include<bits/stdc++.h> using namespace std; typedef long long LL; inl ...
- 四大IO抽象类
四大IO抽象类 InputStream/OutputStream和Reader/writer类是所有IO流类的抽象父类,我们有必要简单了解一下这个四个抽象类的作用.然后,通过它们具体的子类熟悉相 ...
- uwsgi配置文件
[uwsgi] http = :9000 #the local unix socket file than commnuincate to Nginx #socket端口这个用作nginx与其通讯 s ...