531. Bonnie and Clyde

题目连接:

http://acm.sgu.ru/problem.php?contest=0&problem=531

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

Hint

题意

在一条街上的有n个银行,银行在xi位置,有ai元,然后有两个抢劫犯

你需要找两个相距不小于d的银行,使得这两个银行的权值加起来最大

题解:

我是线段树加二分就好了,枚举每一个银行,然后再查询d距离以为的最大银行权值就好了

代码

#include<bits/stdc++.h>
using namespace std; typedef pair<int,int> SgTreeDataType;
struct treenode
{
int L , R ;
SgTreeDataType sum;
}; treenode tree[1001500];
inline void build_tree(int L , int R , int o)
{
tree[o].L = L , tree[o].R = R;
if(L==R)
tree[o].sum = make_pair(0,L);
if (R > L)
{
int mid = (L+R) >> 1;
build_tree(L,mid,o*2);
build_tree(mid+1,R,o*2+1);
if(tree[o*2].sum.first>=tree[o*2+1].sum.first)
tree[o].sum = tree[o*2].sum;
else
tree[o].sum = tree[o*2+1].sum;
}
} inline void updata(int QL,int QR,int v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) tree[o].sum.first = v;
else
{
int mid = (L+R)>>1;
if (QL <= mid) updata(QL,QR,v,o*2);
if (QR > mid) updata(QL,QR,v,o*2+1);
if(tree[o*2].sum.first>=tree[o*2+1].sum.first)
tree[o].sum = tree[o*2].sum;
else
tree[o].sum = tree[o*2+1].sum;
}
}
int ans = 0;
inline SgTreeDataType query(int QL,int QR,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) return tree[o].sum;
else
{
int mid = (L+R)>>1;
SgTreeDataType res = make_pair(0,0);
if (QL <= mid)
{
pair<int,int> T = query(QL,QR,2*o);
if(T.first>=res.first)
res = T;
}
if (QR > mid)
{
pair<int,int> T = query(QL,QR,2*o+1);
if(T.first>=res.first)
res = T;
}
return res;
}
} vector<int> V;
int x[200005],v[200005];
int main()
{
int n,d;
scanf("%d%d",&n,&d);
build_tree(1,n,1);
V.push_back(-1);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x[i],&v[i]);
V.push_back(x[i]);
updata(i,i,v[i],1);
}
if(x[n]-x[1]<d)
return puts("-1 -1");
int ans = 0;
int ans1=0,ans2 =0 ;
for(int i=1;i<=n;i++)
{
int x1 = lower_bound(V.begin(),V.end(),x[i]+d)-V.begin();
if(x1==n+1)break;
pair<int,int> T = query(x1,n,1);
if(T.first + v[i] >= ans)
{
ans = T.first + v[i];
ans1 = T.second,ans2 = i;
}
}
cout<<ans1<<" "<<ans2<<endl;
}

最新文章

  1. .NET 程序集与命名空间
  2. AgileEAS.NET SOA 中间件平台.Net Socket通信框架-简单例子-实现简单的服务端客户端消息应答
  3. Haproxy图解
  4. (转载)常用JS加密编码算法 五:SHA1算法
  5. TCP/IP笔记(二)TCP/IP简介
  6. Mac和Windows系统下Mysql数据库的导入导出
  7. javascript排序算法-快速排序
  8. Codeforces 785 - A/B/C/D/E - (Undone)
  9. 在微信小程序中将获取到的经纬度(经度纬度)转地址(地名)
  10. html 自定义上传图片样式,并回显
  11. linux内存源码分析 - 零散知识点
  12. webDriver文档阅读笔记
  13. Flash芯片你都认识吗?
  14. POJ 2533 裸的LIS
  15. 如何利用Hadoop存储小文件
  16. Linux下查看与修改mtu值
  17. docker link 过时不再用了?那容器互联、服务发现怎么办?
  18. MySQL数据库----基础操作
  19. Android系统广播处理机制
  20. 【Java】返回长度为零的数组或者集合,而不是null

热门文章

  1. bat 批处理脚本
  2. 动态定义数组 .xml
  3. c/c++ 数字转成字符串, 字符串转成数字
  4. kali 安装完成后,无法进入界面
  5. CentOS安装中文输入法:ibus
  6. CodeIgniter 3.0+ 部署linux环境 session报错
  7. [WebService]之TCPMon的使用
  8. 省时的浏览器同步测试工具 browsersync NodeJS
  9. c#装B指南
  10. 基于Storm 分布式BP神经网络,将神经网络做成实时分布式架构