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的核心思想.加 ...
随机推荐
- 高级同步器:同步屏障CyclicBarrier
引自:http://ifeve.com/concurrency-cyclicbarrier/ 简介 CyclicBarrier 的字面意思是可循环使用(Cyclic)的屏障(Barrier).它要做的 ...
- java程序执行系统命令
String cmd="orakill orcl 1233";//解锁数据库表 Process proc = Runtime.getRuntime().exec(cmd);
- java服务端项目开发规范
更新内容 2015-03-13 (请先更新svn的mybatis.xml.BaseMapper.java.Pager.java文件) 加入测试类规范 加入事物控制规范 加入mapper接口规则 ...
- python2.7入门---面向对象
Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.所以,这篇文章我们来记录下Python的面向对象编程.如果你以前没有接触过面向对象的编 ...
- PS作业
- 如何在Centos7下升级Apache至最新版本
Apache是使用最广泛的应用部署软件.并且它也是所有服务器的必要组成部分.安装最新版本的apache意味着拥有更多最新的功能和修复了已知的BUG. 介绍 在这篇教程里面,我将会介绍在Centos7下 ...
- Linux 文件属性及修改权限
输入 ll 或 ls -l 命令显示当前目录中文件的属性及文件所属的用户和组 root@user:/home/www# ll test total 880 drwxr-xr-x 2 root root ...
- python学习总结---网络编程
网络编程 相关概念 - OSI七层模型:它从低到高分别是:物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. - TCP/IP: 在OSI七层模型基础上简化抽象出来的一套网络协议簇,现在得到 ...
- CodeForces-1132C Painting the Fence
题目链接 https://vjudge.net/problem/CodeForces-1132C 题面 Description You have a long fence which consists ...
- Leetcode 3. Longest Substring Without Repeating Characters (Medium)
Description Given a string, find the length of the longest substring without repeating characters. E ...