SGU 531 - Bonnie and Clyde 预处理+二分
Bonnie and Clyde
Description
Bonnie and Clyde are into robbing banks. This time their target is a town called Castle Rock. There are n banks located along Castle Rock's main street; each bank is described by two positive integers xi, wi, where xi represents the distance between the i-th bank and the beginning of the street and wi represents how much money the i-th bank has. The street can be represented as a straight line segment, that's why values of xi can be regarded as the banks' coordinates on some imaginary coordinate axis.
This time Bonnie and Clyde decided to split, they decided to rob two different banks at a time. As robberies aren't exactly rare in Castle Rock, Bonnie and Clyde hope that the police won't see the connection between the two robberies. To decrease the chance of their plan being discovered by the investigation, they decided that the distance between the two robbed banks should be no less than d.
Help Bonnie and Clyde find two such banks, the distance between which is no less than d and the sum of money in which is maximum.
Input
The first input line contains a pair of integers n, d (1 ≤ n ≤ 2 · 105, 1 ≤ d ≤ 108), where n is the number of banks and d is the minimum acceptable distance between the robberies. Then n lines contain descriptions of banks, one per line. Each line contains two integers xi, wi (1 ≤ xi,wi ≤ 108), xi shows how far the i-th bank is from the beginning of the street and wi shows the number of money in the bank. Positions of no two banks coincide. The banks are given in the increasing order of xi.
Output
Print two integer numbers — indicies of the required banks. The banks are numbered starting from 1 in the order in which they follow in the input data. You may print indicies in any order. If there are many solutions, print any of them. If no such pair of banks exists, print "-1 -1" (without quotes).
Sample Input
6 3
1 1
3 5
4 8
6 4
10 3
11 2
Sample Output
5 3
题意
给你n个银行,每次银行的位置x[i],金钱w[i], 现在让你选择两个不同的银行 使其距离大于等于D 且金钱和最大
输出选择的那两个银行,序号
题解:
我是先预处理出 i ~n 中 金钱最多的,序号是那个
再遍历一次, 二分当前x[i]+ d的 序号就好了, 即 与其距离满足大于等于d且 金钱最多的是哪一个银行 ,更新答案
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std ;
typedef long long ll; const int N = + ;
const ll inf = ; ll x[N], w[N], M[N], H[N],n,d;
void init() {
M[n+] = -;
H[n + ] = n+;
for(int i = n; i >= ; i--) {
if(w[i] >= M[i + ]) M[i] = w[i], H[i] = i;
else M[i] = M[i+], H[i] = H[i + ];
// cout<<M[i]<<" "<<H[i]<<endl;
}
}
int main() {
scanf("%I64d%I64d",&n,&d);
for(int i = ; i <= n; i++) {
scanf("%I64d%I64d",&x[i],&w[i]);
}
x[n + ] = inf; w[n + ] = -inf;
init();
ll ans = , ansl = -, ansr = -;
for(int i = ; i <= n; i++) {
ll tmp = x[i] + d;
int pos = lower_bound(x + , x + n + , tmp) - x;
if(pos == n + ) break;
if(w[i] + w[H[pos]] > ans ) ans = w[i] + w[H[pos]], ansl = i, ansr = H[pos];
}
printf("%I64d %I64d\n",ansl,ansr);
return ;
}
代码
SGU 531 - Bonnie and Clyde 预处理+二分的更多相关文章
- SGU 531. Bonnie and Clyde 线段树
531. Bonnie and Clyde 题目连接: http://acm.sgu.ru/problem.php?contest=0&problem=531 Description Bonn ...
- loj 1150(spfa预处理+二分+最大匹配)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26864 思路:首先是spfa预处理出每个'G'到'H'的最短距离, ...
- poj 3501 Escape from Enemy Territory 预处理+二分+bfs
传送门 给一个起点一个终点, 给出整个地图的宽和高, 给出n个敌人的坐标. 让你找到一条路径, 这条路径上的点距离所有敌人的距离都最短, 输出最短距离. 首先预处理出来地图上的所有点到敌人的最短距离, ...
- HDU 5878 I Count Two Three (预处理+二分查找)
题意:给出一个整数nnn, 找出一个大于等于nnn的最小整数mmm, 使得mmm可以表示为2a3b5c7d2^a3^b5^c7^d2a3b5c7d. 析:预处理出所有形为2a3 ...
- 2019CCPC-江西省赛C题 HDU6569 GCD预处理+二分
Trap Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Subm ...
- hdu_5968_异或密码(预处理+二分)
题目链接:hdu_5968_异或密码 题意: 中午,不解释 题解: 前缀处理一下异或值,然后上个二分查找就行了,注意是unsigned long long #include<bits/stdc+ ...
- 51Nod 1010 只包含因子2 3 5的数 | 预处理+二分
Input示例 5 1 8 13 35 77 Output示例 2 8 15 36 80 分析:将所有的只含有2 3 5因子的数打一个表保存在一个数组里,然后二分查找第一个>=数组里的数,输出 ...
- hackerrank [Week of Code 33] Bonnie and Clyde
任意门 题意:给一个图,每次询问给三个点a,b,c,问是否存在一条从a到c,一条b到c的路径除c外无交点. 双连通分量缩点建出圆方树是必须的,然后我们需要判断c是否在a到b的路径上,或者c的某个相邻的 ...
- HDU 5042 GCD pair 预处理+二分 分段
点击打开链接 #include <stdio.h> #include <string.h> #include <iostream> #include <cma ...
随机推荐
- 一篇个人感觉比较好的lua入门的文章
原文转自www.cppprog.com,由三篇文章组成 Lua是一个嵌入式的脚本语言,它不仅可以单独使用还能与其它语言混合调用.Lua与其它脚本语言相比,其突出优势在于: 1. 可扩展性.Lua的扩 ...
- Hadoop MapReduce编程 API入门系列之最短路径(十五)
不多说,直接上代码. ======================================= Iteration: 1= Input path: out/shortestpath/input. ...
- Oracle性能优化——总体介绍
最近参加Oracle的培训,对Oracle有了更加深入的认识,在此做个学习总结. 1.Oracle数据库调优不能仅指望修改几项数据库参数就能有明显效果,问题更多出在应用方面,教育开发者正确地使用数据库 ...
- ubuntu16 mysql 远程连接
打开配置文件: sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf 将下面一行注释掉: # bind-address = localhost 重启服务 sudo s ...
- dotnetnuke7.3.3 下弹出对话框(dnnConfirm())的使用
今天用dnn做一个列表里边有一个删除操作,就想做个对话框确定是否删除? 正常理解马上想到js的confirm("")函数,但是发现Dnn把这个函数给重写啦,弹出的对话框竟然是英文的 ...
- CUDA5.5入门文章:VS10设置
原文链接:http://blog.csdn.net/augusdi/article/details/12205435 作者专栏:http://blog.csdn.net/augusdi/article ...
- MVC 先后顺序
@foreach (var item in Model) { if (ViewBag.GetModel.ParentID == item.DictID) { <option value=&quo ...
- H3C交换机telnet服务认证模式配置
以H3C交换机为例,介绍telnet服务的三种认证方式配置(none无需认证,password密码认证,scheme账户+密码认证) None认证模式配置步骤:[H3C]telnet server e ...
- Exact Change FreeCodeCamp
function checkCashRegister(price, cash, cid) { var change; var sumCid = 0; // Here is your change, m ...
- 洛谷P2038 无线网络发射器选址 水题 枚举
刚开始边界写错了(将128写成127). 注意n <= 20,所以可以每读入一个点就将其周边更新,这样最多也只会有 40 * 40 * 20 种位置需要被枚举. Code: #include&l ...