Fountains
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 n, c 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.
Examples
Input
3 7 6 10 8 C 4 3 C 5 6 D
Output
9
Input
2 4 5 2 5 C 2 1 D
Output
0
Input
3 10 10 5 5 C 5 5 C 10 11 D
Output
10
Note
In the first example Arkady should build the second fountain with beauty 4, which costs 3 coins. The first fountain he can't build because he don't have enough coins. Also Arkady should build the third fountain with beauty 5 which costs 6 diamonds. Thus the total beauty of built fountains is 9.
In the second example there are two fountains, but Arkady can't build both of them, because he needs 5 coins for the first fountain, and Arkady has only 4 coins.
题意:输入 n c d, n 代表有 n 个喷泉,c 代表你拥有的金币数,d 代表你拥有的钻石数,随后有 n 行,每一行有3个值, b q ch,代表这个喷泉的 美丽值,建造需要的钱的数量,是用金币建造还是用钻石建造。。。 金币和钻石不能互换,一个喷泉只能建造一次,问最后能够获得的最大美丽, 如果不能建造两个喷泉 输出 -1;
思路:很显然,获得美丽有三种方案,1. 全部用金币建造, 2.全部用钻石建造,3.金币建造一个,钻石建造一个。 所以很简单能够用 钱最为下标 来 构造线段树,但是有一个问题就是如何避免 不重复建造。。。蒟蒻的我被卡在了这里,QAQ,方法就是先查询后更新。。。
我抄袭的AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
struct xx{
int b,p;
}C[maxn],D[maxn];
int tree[maxn << 2];
bool cmp(xx A,xx B){
return A.b < B.b;
}
void build(int l,int r,int i){
tree[i] = 0;
if(l == r)
return ;
int mid = (l + r) >> 1;
build(l,mid,i << 1);
build(mid + 1,r,i << 1 | 1);
}
void update(int l,int r,int i,int mo,int be){
if(l == r)
tree[i] = max(tree[i],be);
else {
int mid = (l + r) >> 1;
if(mo <= mid) update(l,mid,i << 1,mo,be);
else update(mid + 1,r,i << 1|1,mo,be);
tree[i] = max(tree[i<<1],tree[i<<1|1]);
}
}
int query(int ql,int qr,int l,int r,int i){
if(ql <= l && r <= qr)
return tree[i];
int mid = (l + r) >> 1;
int ans = 0;
if(ql <= mid) ans = max(ans,query(ql,qr,l,mid,i << 1));
if(qr > mid) ans = max(ans,query(ql,qr,mid + 1,r,i << 1 | 1));
return ans;
}
int main()
{
int n,c,d;
while(~scanf("%d%d%d",&n,&c,&d)){
int ccnt = 0,dcnt = 0;
int b,p; char ch;
int maxc = -1,maxd = -1;
for(int i = 0;i < n;i ++){
scanf("%d%d %c",&b,&p,&ch);
if(ch == 'C'){
if(p <= c){
C[ccnt].b = b, C[ccnt ++].p = p;
if(maxc < C[ccnt - 1].b) maxc = C[ccnt - 1].b;
}
}
else {
if(p <= d){
D[dcnt].b = b, D[dcnt ++].p = p;
if(maxd < D[dcnt - 1].b) maxd = D[dcnt - 1].b;
}
}
}
sort(C,C + ccnt,cmp); sort(D,D + dcnt,cmp);
int maxx = (maxc == -1 || maxd == -1) ? -1 : (maxc + maxd);
int ok1 = maxx == -1 ? 0 : 1,ok2 = 0, ok3 = 0;
build(1,100000,1);
int tmp = 0,tmp1 = 0;
for(int i = 0;i < ccnt;i ++){
tmp = C[i].b; tmp1 = 0;
if(c != C[i].p) tmp1 = query(1,c - C[i].p,1,100000,1);
if(tmp1 != 0){
ok2 = 1;
maxx = max(maxx,tmp + tmp1);
}
update(1,100000,1,C[i].p,C[i].b);
}
build(1,100000,1);
for(int i = 0;i < dcnt;i ++){
tmp = D[i].b; tmp1 = 0;
if(d != D[i].p) tmp1 = query(1,d - D[i].p,1,100000,1);
if(tmp1 != 0){
ok3 = 1;
maxx = max(maxx,tmp + tmp1);
}
update(1,100000,1,D[i].p,D[i].b);
}
if(ok1 || ok2 || ok3) printf("%d\n",maxx);
else printf("0\n");
}
return 0;
}