SGU531 Bonnie and Clyde RMQ

来源:互联网 发布:知天气哪个版本好 编辑:程序博客网 时间:2024/06/07 01:37

A. Bonnie and Clyde
time limit per test3 seconds
memory limit per test256 megabytes
inputinput.txt
outputoutput.txt
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).

Examples
input
6 3
1 1
3 5
4 8
6 4
10 3
11 2
output
5 3

题意:
给你银行的个数n和距离d
接下来是n个银行的坐标和拥有的钱
两个大盗去抢两个银行,并且这两个银行的距离不能小于d
问为了能到最多的钱的,应该选择哪两个银行

#include<bits/stdc++.h>using namespace std;const int Maxn = 1e6;int X[Maxn], w[Maxn], dp[Maxn][20];void init(int N) {    for (int i = 1; i <= N; i++)        dp[i][0] = i;    for (int j = 1; (1 << j) <= N; j++)        for (int i = 1; i + (1 << j) - 1 <= N; i++) {            int a = dp[i][j - 1], b = dp[i + (1 << (j - 1))][j - 1];            dp[i][j] = w[a] > w[b] ? a : b;        }}int query(int i, int j) {    int k = (int)(log(j - i + 1) / log(2));    int a = dp[i][k], b = dp[j - (1 << k) + 1][k];    return  w[a] > w[b] ? a : b;}int main() {    int n, d;    freopen("input.txt", "r", stdin);    freopen("output.txt", "w", stdout);    while (cin >> n >> d) {        for (int i = 1; i <= n; i++)            scanf("%d%d", &X[i], &w[i]);        init(n);        if (n == 1) {            cout << "-1 -1\n" << endl;            continue;        }        int x = -1, y = -1, MAXN = -1;        for (int i = 2; i <= n; i++) {            if (X[i] - X[1] < d)                continue;            int k = upper_bound(X + 1, X + 1 + n, X[i] - d) - X;            if (k == 1) continue;            int c = query(1, k - 1);            if (w[i] + w[c] > MAXN) {                MAXN = w[i] + w[c];                x = c, y = i;            }        }        printf("%d %d\n", x, y);    }}
0 0
原创粉丝点击