【倍增】Tak and Hotels II @ABC044&ARC060/upcexam6463
6463: Tak and Hotels II
时间限制: 1 Sec 内存限制: 128 MB
题目描述
Tak the traveler has the following two personal principles:
He never travels a distance of more than L in a single day.
He never sleeps in the open. That is, he must stay at a hotel at the end of a day.
You are given Q queries. The j-th (1≤j≤Q) query is described by two distinct integers aj and bj. For each query, find the minimum number of days that Tak needs to travel from the aj-th hotel to the bj-th hotel following his principles. It is guaranteed that he can always travel from the aj-th hotel to the bj-th hotel, in any given input.
Constraints
2≤N≤105
1≤L≤109
1≤Q≤105
1≤xi<x2<…<xN≤109
xi+1−xi≤L
1≤aj,bj≤N
aj≠bj
N,L,Q,xi,aj,bj are integers.
Partial Score
200 points will be awarded for passing the test set satisfying N≤103 and Q≤103.
输入
N
x1 x2 … xN
L
Q
a1 b1
a2 b2
:
aQ bQ
输出
样例输入
9
1 3 6 13 15 18 19 29 31
10
4
1 8
7 3
6 7
8 5
样例输出
4
2
1
2
提示
For the 1-st query, he can travel from the 1-st hotel to the 8-th hotel in 4 days, as follows:
Day 1: Travel from the 1-st hotel to the 2-nd hotel. The distance traveled is 2.
Day 2: Travel from the 2-nd hotel to the 4-th hotel. The distance traveled is 10.
Day 3: Travel from the 4-th hotel to the 7-th hotel. The distance traveled is 6.
Day 4: Travel from the 7-th hotel to the 8-th hotel. The distance traveled is 10.
题意:给你直线上一些点的坐标,一天最大的移动距离,要求每天结束时必须在一个点上。1e5次询问,每次询问从第a个点到第b个点最少要几天。solutionf[x][y]表示第x个点2^y次方的天数能到的最远点。暴力或二分预处理f[x][0],然后 f[x][i] = f[f[x][i-1]][i-1];查询的时候, 找到从cur(当前点)出发的第一个小于b位置的f[cur][j],最后答案加上1(2^0)code
#define IN_LB() freopen("C:\\Users\\acm2018\\Desktop\\in.txt","r",stdin)
#define OUT_LB() freopen("C:\\Users\\acm2018\\Desktop\\out.txt","w",stdout)
#define IN_PC() freopen("C:\\Users\\hz\\Desktop\\in.txt","r",stdin)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100005;
int ind[maxn],lnh,qry;
const int INF = 1<<30;
int f[maxn][35];
int main() {
// IN_LB();
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++) {
scanf("%d",ind+i);
}
scanf("%d%d",&lnh,&qry);
for(int i=1; i<=n; i++) {
int idx = (int)(upper_bound(ind+1,ind+n+1, ind[i]+lnh)-ind-1);
f[i][0] = idx;
}
for(int j=1; j<=30; j++) {
for(int i=1; i<=n; i++) {
f[i][j] = f[f[i][j-1]][j-1];
}
}
for(int q=0; q<qry; q++) {
int a,b;
scanf("%d%d",&a,&b);
if(a>b)
swap(a,b);
int ans = 0,cur = a;
for(int i=30;i>=0;i--){
if(f[cur][i]<b){
ans+=(1<<(i));
cur = f[cur][i];
}
}
printf("%d\n",ans+1);
}
return 0;
}
【倍增】Tak and Hotels II @ABC044&ARC060/upcexam6463的更多相关文章
- 高橋君とホテル / Tak and Hotels
高橋君とホテル / Tak and Hotels Time limit : 3sec / Stack limit : 256MB / Memory limit : 256MB Score : 700 ...
- AtCoder Beginner Contest 044 A - 高橋君とホテルイージー / Tak and Hotels (ABC Edit)
Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement There is a hotel with ...
- 2018.09.17 atcoder Tak and Hotels(贪心+分块)
传送门 一道有意思的题. 一开始想错了,以为一直lowerlowerlower_boundboundbound就可以解决询问,结果交上去TLE了之后才发现时间复杂度是错的. 但是贪心思想一定是对的,每 ...
- AtCoder Tak and Hotels
题目链接:传送门 题目大意:有 n 个点排成一条直线,每次行动可以移动不超过 L 的距离,每次行动完成必须停在点上, 数据保证有解,有 m 组询问,问从 x 到 y 最少需要几次行动? 题目思路:倍增 ...
- 【AtCoder】ARC060
ARC060 C - 高橋君とカード / Tak and Cards 每个数减去A,然后转移N次,每次选或不选,最后是和为0的时候的方案数,负数可以通过把所有数右移2500做到 #include &l ...
- AtCoder Regular Contest
一句话题解 因为上篇AGC的写的有点长……估计这篇也短不了所以放个一句话题解方便查阅啥的吧QwQ 具体的题意代码题解还是往下翻…… ARC 058 D:简单容斥计数. E:用二进制表示放的数字,然后状 ...
- AtCoder 杂题训练
前言: 因为要普及了,今年没一等就可以退役去学文化课了,所以暑假把历年noip普及组都刷了一遍,离noip还有50+天,想弄点强化训练什么的. 想了想,就这些天学文化课之余有空就把AtCoder之前那 ...
- AtCoder-arc060 (题解)
A - 高橋君とカード / Tak and Cards (DP) 题目链接 题目大意: 有 \(n\) 个数字,要求取出一些数字,使得它们的平均数恰好为 \(x\) ,问有几种取法. 大致思路: 只要 ...
- AtCoder Regular Contest 060
C - 高橋君とカード / Tak and Cards 思路:dp,先说说我想的,我写的dp数组是dp[i][j][k],表示从前i个数字中,选择j个数字,平均值为k,则dp[i][j][k] = d ...
随机推荐
- Oracle Instant Client(即时客户端) 安装与配置
一.下载 下载地址:http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html 这是Ora ...
- vue中methods一个方法调用另外一个方法
转自http://blog.csdn.net/zhangjing1019/article/details/77942923 vue在同一个组件内: methods中的一个方法调用methods中的另外 ...
- 清北合肥day2-day5
day2:215这一天的题目相对比较模板化t1:50看错了数据范围求n个点到给出的点哈夫曼距离的最小值我想到的是一种非常zz的做法我们二分答案,然后判断是否在这个距离内有点但是这样前缀和不是很好维护于 ...
- 【AtCoder】ARC075
ARC075 在省选前一天听说正式选手线画到省二,有了别的女选手,慌的一批,然后刷了一个ARC来稍微找回一点代码感觉 最后还是挂分了,不开心 果然水平退化老年加重啊 原题链接 C - Bugged 直 ...
- html5的audio实现高仿微信语音播放效果Demo
HTML部分: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <t ...
- 获取Form表单数据转化成JSON对象
$.fn.serializeObject = function() { var o = {}; var a = this.serializeArray(); $.each(a, function() ...
- BZOJ1396 识别子串 字符串 SAM 线段树
原文链接http://www.cnblogs.com/zhouzhendong/p/9004467.html 题目传送门 - BZOJ1396 题意 给定一个字符串$s$,$|s|\leq 10^5$ ...
- PyQt PySide QListWidget 添加自定义 widget
PyQt PySide QListWidget 添加自定义 widget 原文链接:https://stackoverflow.com/questions/25187444/pyqt-qlistwid ...
- Mysql数据库报错:Cannot add or update a child row: a foreign key constraint fails(添加多对多关系)
#创建班级表 class Classes(models.Model): title = models.CharField(max_length=32) n=models.ManyToManyField ...
- Django 学习第六天——Django模型基础第一节
一.Django 的 ORM 简介: Django的ORM系统的分析: 1.ORM 概念:对象关系映射(Object Relational Mapping,简称ORM) 2.ORM的优势:不用直接编写 ...