SGU 531. Bonnie and Clyde 线段树
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;
}
SGU 531. Bonnie and Clyde 线段树的更多相关文章
- SGU 531 - Bonnie and Clyde 预处理+二分
Bonnie and Clyde Description Bonnie and Clyde are into robbing banks. This time their target is a to ...
- SGU 311. Ice-cream Tycoon(线段树)
311. Ice-cream Tycoon Time limit per test: 0.5 second(s)Memory limit: 65536 kilobytes input: standar ...
- SGU 319 Kalevich Strikes Back(线段树扫描线)
题目大意: n个矩形,将一个大矩形分成 n+1 块.矩形之间不重合,可是包括.求这n+1个矩形的面积 思路分析: 用线段树记录他们之间的父子关系.然后dfs 计算面积. 当给出的矩形上边的时候,就要记 ...
- SGU 180 Inversions(离散化 + 线段树求逆序对)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...
- SGU 319. Kalevich Strikes Back (线段树)
319. Kalevich Strikes Back Time limit per test: 0.5 second(s)Memory limit: 65536 kilobytes input: st ...
- SGU - 311 Ice-cream Tycoon(线段树)
Description You've recently started an ice-cream business in a local school. During a day you have m ...
- bzoj3932--可持久化线段树
题目大意: 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第 ...
- codevs 1082 线段树练习 3(区间维护)
codevs 1082 线段树练习 3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...
- codevs 1576 最长上升子序列的线段树优化
题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...
随机推荐
- 【,net】发布网站问题
今天解决了一个发布后网站访问不了的问题: 问题: 发布网站:http://172.168.1.102:2222/nologin.html,但是页面一直处于刷新状态,进行不了系统: 解决方法: 问开发他 ...
- TestNG中同一个类中执行多个test()方法如何配置testng.xml
public class IndexInfo extends BaseTesting{ private IndexPage IndexPage1;// private AddEquipmentInfo ...
- JavaScript专业规则12条
学习JavaScript是困难的.它发展的如此之快,以至于在任何一个特定的时刻,你都不清楚自己是否“做错了”.有些时候,感觉像是坏的部分超过了好的部分.然而,讨论这些并没有意义,JavaScript正 ...
- dispatch_once单例初始化
static GHCache *instance = nil; /** * 单例,静态初始化方法 * * @return 返回一个单例 */ + (GHCache*)shareCache{ sta ...
- [cocos2d-js]长按按钮事件
定义两个全局变量 var bLeftButtonClick = false; var bRightButtonClick = false; var MainLayer = cc.Layer.exten ...
- js运动 多数据运动 含JSON
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 轻松学习Linux之AWK使用初步
AWK最初是Unix平台上一种可以对文本进行逐行处理的编程语言,它来源于3个创作者的名字:Aho.(Peter)Weinberg和(Brain)Kernighan. 现在广泛应用于Linux,他与se ...
- rdlc 分页操作
工具箱中拖一个列表过来,设置 列表-->行组-->组属性常规-->组表达式=Int((RowNumber(Nothing)-1)/10)分页符-->勾选在组的结尾
- vim之pydiction插件
[vim之pydiction插件] It consists of three main files: python_pydiction.vim -- Vim plugin. complete-dict ...
- #ifdef __cplusplus
转自:http://www.2cto.com/kf/201302/191822.html #ifdef __cplusplus,一般用于将C++代码以标准C形式输出(即以C的形式被调用),这是因为C+ ...