【倍增】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 ...
随机推荐
- noip飞扬的小鸟
题解: 挺简单的题目 f[i][j]表示x坐标为i,y坐标为j的最小值 会发现那个东西是个完全背包 从f[i][j-a[i]]转移一下就是O(1)转移的了 另外上界为m这个要特判一下 我把sum[a[ ...
- alpha冲刺1/10
目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:来自双十一的爱 团队部分 后敬甲(组长) 过去两天完成了哪些任务 文字描述 Alpha版本的任务细分安排 leangoo ...
- linux下几个常用软件
Ubuntu 软件包地址 https://packages.ubuntu.com/ 一. 字体 不管是雅黑还是宋体,从windows cp过来后, 直接双击打开并安装 二. Meld 可视的diff和 ...
- 【Android】修改Android 模拟器IMEI
修改Android 模拟器IMEI 在.....\android_sdk\tools文件下找到emulator-arm.exe,使用UltraEdit文本编辑器打开,搜索CGSN关键字,把000000 ...
- linux脚本实现scp命令自动输入密码和yes/no等确认信息
实现方式: 通过expect工具实现 #!/bin/bash yum -y install expect expect -c " spawn scp -r root@192.168.10.1 ...
- HDU5692 Snacks DFS序 线段树
去博客园看该题解 题目 HDU5692 Snacks Problem Description 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的 ...
- SSL/TLS
為 授权计算机为 SSL/TLS 安全通道建立信任关系. ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, e ...
- server client 套接字连接
server端: 1. 阻塞型套接字,不能满足多个客户端同时访问 import socket server = socket.socket() server.bind((""127 ...
- 589. N叉树的前序遍历
[题目] 给定一个 N 叉树,返回其节点值的前序遍历. 例如,给定一个 3叉树 : 返回其前序遍历: [1,3,5,6,2,4]. [解析] """ # Definiti ...
- 牛客练习赛 26 C题 城市规划【贪心】
<题目链接> 题目描述 小a的国家里有n个城市,其中第i和第i - 1个城市之间有无向道路连接,特殊的,第1个城市仅与第2个城市相连为了减轻道路维护负担,城市规划局局长MXT给出了m个要求 ...