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 ...
随机推荐
- 使用C语言扩展Python3
使用C语言扩展Python3.在Python3中正确调用C函数. 1. 文件demo.c #include <Python.h> // c function static PyObject ...
- css3实现3D切割轮播图案例
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- (转载)Activity-数据状态的保存
由于手机是便捷式移动设备,掌握在用户的手中,它的展示方向我们是无法预知的,具有不确定性.平时我们拿着手机多数为竖屏,但有时候我们感觉累了也会躺着去使用手机,那么这时手机屏幕的展示方向可能已经被用户切换 ...
- 二.Windows I/O模型之异步选择(WSAAsyncSelect)模型
1.基于windows消息为基础的网络事件io模型.因此我们必须要在窗口程序中使用该模型.该模型中的核心是调用WSAAsyncSelect函数实现异步I/O. 2.WSAAsyncSelect函数:注 ...
- 【Oracle】RAC集群中的命令
数据库名称:racdb 节点名称:rac3.rac4 注:以下命令均在grid用户中执行 1.查看集群节点的状态: [grid@rac3 ~]$ crsctl check cluster [grid@ ...
- a better git log
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d ...
- C# HttpWebRequest post 请求传参数
Dictionary<string, string> parameters = new Dictionary<string, string>(); //参数列表 paramet ...
- YoC云上芯片家族迎来新成员
Espressif 乐鑫信息科技(以下简称乐鑫科技)近日在上海召开发布会,发布其旗下最新的旗舰同时也是第二代Yun on Chip(简称YoC)云上芯片ESP32.YoC云上芯片是由YunOS牵头,联 ...
- (转)基于MVC4+EasyUI的Web开发框架经验总结(3)- 使用Json实体类构建菜单数据
http://www.cnblogs.com/wuhuacong/p/3669708.html 最近花了不少时间在重构和进一步提炼我的Web开发框架上,力求在用户体验和界面设计方面,和Winform开 ...
- RabbitMQ学习笔记(5)----RabbitMQ整合Spring
在Spring AMQP项目中Spring也提供了对RabbitMQ的支持,这里在之前学习SpringBoot的时候也整合过,但是今天这里使用的Spring的xml配置来整个rabbit. Sprin ...