CodeForces - 799C  树状数组

博客围绕游戏 Gardenscapes 展开,玩家 Arkady 想建两个新喷泉,已知有 n 个喷泉,每个喷泉有其美丽值和成本,成本分为硬币和钻石两种类型且不能转换。需根据 Arkady 拥有的硬币和钻石数量,找出能同时购买的两个喷泉的最大总美丽值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Arkady plays Gardenscapes a lot. Arkady wants to build two new fountains. There are n available fountains, for each fountain its beauty and cost are known. There are two types of money in the game: coins and diamonds, so each fountain cost can be either in coins or diamonds. No money changes between the types are allowed.

Help Arkady to find two fountains with maximum total beauty so that he can buy both at the same time.

Input

The first line contains three integers nc and d (2 ≤ n ≤ 100 000, 0 ≤ c, d ≤ 100 000) — the number of fountains, the number of coins and diamonds Arkady has.

The next n lines describe fountains. Each of these lines contain two integers bi and pi (1 ≤ bi, pi ≤ 100 000) — the beauty and the cost of the i-th fountain, and then a letter "C" or "D", describing in which type of money is the cost of fountain i: in coins or in diamonds, respectively.

Output

Print the maximum total beauty of exactly two fountains Arkady can build. If he can't build two fountains, print 0.

 

#include <iostream>
#include <cstdio>
#include <string.h>
#include <stack>
#include <vector>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <cmath>
using namespace std;
#define LL long long
const int MAX = 1e5 + 50;

int n;
int a[MAX], tree1[MAX], tree2[MAX];

int lowbit(int x){
	return x & (-x);
}

void updata1(int i, int k){
	while(i <= MAX){
		tree1[i] = max(tree1[i], k);
		i += lowbit(i);
	}
}

int getmax1(int i){
	int res = 0;
	while(i > 0){
		res = max(res, tree1[i]);
		i -= lowbit(i);
	}
	return res;
}

void updata2(int i, int k){
	while(i <= MAX){
		tree2[i] = max(k, tree2[i]);
		i += lowbit(i);
	}
}

int getmax2(int i){
	int res = 0;
	while(i > 0){
		res = max(res, tree2[i]);
		i -= lowbit(i);
	}
	return res;
}
int main() 
{
	int c, d;
	scanf("%d%d%d", &n, &c, &d);
	int ans = 0;
	for(int i = 0; i < n; i++){
		int b, p;
		char ch;
		scanf("%d%d %c", &b, &p, &ch);
		if(ch == 'C' && p <= c){
			if(getmax2(d)){ // 因为必须买两个,不能少,所以若买不到第二个,则不更新
				flag = 1;
				ans = max(ans, b + getmax2(d));
			}
			if(c - p > 0){
				if(getmax1(c - p)){
					flag = 1;
					ans = max(ans, b + getmax1(c - p));
				}
			}
			updata1(p, b);
		}

		if(ch == 'D' && p <= d){
			if(getmax1(c)){
				flag = 1;
				ans = max(ans, b + getmax1(c));
			}
			if(d - p > 0){
				if(getmax2(d - p)){
					flag = 1;
					ans = max(ans, b + getmax2(d - p));
				}
			}
			updata2(p, b);
		}
	}
	printf("%d\n", ans);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值