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 预处理+二分的更多相关文章

  1. SGU 531. Bonnie and Clyde 线段树

    531. Bonnie and Clyde 题目连接: http://acm.sgu.ru/problem.php?contest=0&problem=531 Description Bonn ...

  2. loj 1150(spfa预处理+二分+最大匹配)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26864 思路:首先是spfa预处理出每个'G'到'H'的最短距离, ...

  3. poj 3501 Escape from Enemy Territory 预处理+二分+bfs

    传送门 给一个起点一个终点, 给出整个地图的宽和高, 给出n个敌人的坐标. 让你找到一条路径, 这条路径上的点距离所有敌人的距离都最短, 输出最短距离. 首先预处理出来地图上的所有点到敌人的最短距离, ...

  4. HDU 5878 I Count Two Three (预处理+二分查找)

    题意:给出一个整数nnn, 找出一个大于等于nnn的最小整数mmm, 使得mmm可以表示为2a3b5c7d2^a3^b5^c7^d2​a​​3​b​​5​c​​7​d​​. 析:预处理出所有形为2a3 ...

  5. 2019CCPC-江西省赛C题 HDU6569 GCD预处理+二分

    Trap Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Subm ...

  6. hdu_5968_异或密码(预处理+二分)

    题目链接:hdu_5968_异或密码 题意: 中午,不解释 题解: 前缀处理一下异或值,然后上个二分查找就行了,注意是unsigned long long #include<bits/stdc+ ...

  7. 51Nod 1010 只包含因子2 3 5的数 | 预处理+二分

    Input示例 5 1 8 13 35 77 Output示例 2 8 15 36 80 分析:将所有的只含有2 3 5因子的数打一个表保存在一个数组里,然后二分查找第一个>=数组里的数,输出 ...

  8. hackerrank [Week of Code 33] Bonnie and Clyde

    任意门 题意:给一个图,每次询问给三个点a,b,c,问是否存在一条从a到c,一条b到c的路径除c外无交点. 双连通分量缩点建出圆方树是必须的,然后我们需要判断c是否在a到b的路径上,或者c的某个相邻的 ...

  9. HDU 5042 GCD pair 预处理+二分 分段

    点击打开链接 #include <stdio.h> #include <string.h> #include <iostream> #include <cma ...

随机推荐

  1. Python基本数据类型之字符串str

    字符串 定义:它是一个有序的字符的集合,用于存储和表示基本的文本信息,‘’或“”或‘’‘ ’‘’中间包含的内容称之为字符串 字符串的结构类型为'...' "..." "' ...

  2. NOIP 2010 关押罪犯 并查集 二分+二分图染色

    题目描述: S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值" ...

  3. JS实时获取浏览器窗口尺寸 .

    给div实时设置宽度 <div id="detail" style="width: 100%; overflow: scroll;"> </d ...

  4. adb 通过 WiFi 连接 Android 设备

    PC 和 Android 设备连接在同一个局域网. 查看 Android 设备的 IP:设置 > WLAN > 选择连接的WiFi > 查看IP地址. PC 端执行: ping &l ...

  5. 11.05 选择前n个记录

    select ename,salfrom(select (select count(distinct b.sal)from emp bwhere a.sal<=b.sal) as rnk,a.s ...

  6. CDR中是否有图层,如何调出图层面板?

    什么是图层?如果有点PS基础的同学,应该会非常清楚这个概念,它是构成图像的重要组成单位,许多效果可以通过对层的直接操作而得到,并在当前图层操作时候不会影响到其他图层,所以在绘图的过程中有着很重要的作用 ...

  7. BZOJ 1305: [CQOI2009]dance跳舞 网络最大流_二分答案_建模

    Description 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢,而其他相互不喜欢(不会 ...

  8. 路飞学城Python-Day101

    57-多表操作之一对多添加纪录 def add(request): # pub = Publish.objects.create(name='人民出版社', email='873245193@qq.c ...

  9. prevent阻止标签默认行为&stop阻止事件冒泡

    <form id="vm" v-on:submit.prevent="register"> 1.prevent是preventDefault,阻止标 ...

  10. Java 分布式事务

    0 引言 本文主要介绍java中分布式事务以及对应的解决方案. 1 分布式事务产生的原因 1.1 数据库分库分表 当数据库单表一年产生的数据超过1000W,那么就要考虑分库分表,具体分库分表的原理在此 ...