C. Fountains
time limit per test2 seconds
memory limit per test256 megabytes
Arkady plays Gardenscapes a lot. Arkady wants to build two newfountains. There are n available fountains, for each fountain its beauty and cost areknown. There are two types of money in the game: coins and diamonds, so eachfountain cost can be either in coins or diamonds. No money changes between thetypes are allowed.
Help Arkady to find two fountains with maximum total beauty sothat he can buy both at the same time.
Input
The first line contains three integersn,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 nextn lines describe fountains. Each of these lines contain twointegers 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 ofmoney is the cost of fountain i: in coins or in diamonds, respectively.
Output
Printthe maximum total beauty of exactly two fountains Arkady can build. If he can'tbuild 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 fountainwith beauty 4, which costs 3 coins. The first fountain he can't build because he don't haveenough coins. Also Arkady should build the third fountain with beauty5 which costs 6 diamonds. Thus the total beauty ofbuilt fountains is 9.
Inthe second example there are two fountains, but Arkady can't build both ofthem, because he needs 5 coins for the first fountain, and Arkady has only 4 coins.
题意:有n个喷泉,你有c个硬币和d颗宝石,建造每个喷泉需要p个硬币或宝石(只有其一),每个喷泉有一定的beauty值,问使用所拥有的硬币和宝石建造两个不同的喷泉所能达到的最大beauty值(若无法建造两个喷泉,则输出0)
思路:按照建造花费方式分成两堆,先各自计算每一堆中所能达到的最大值,相加得到一个值,然后在分别计算一堆中建两个喷泉所能达到的最大值,三个值取最大值即可(用树状数组实现)
#include <bits/stdc++.h>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);++i)
#define ll long long
const int maxn = 100005;
const int mod = 475;
const ll INF = 0x3f3f3f3f;
const double eps = 1e-6;
#define rush() int T;scanf("%d",&T);while(T--)
int tree[maxn];
int n;
struct node
{
int b,p;
}a[maxn],b[maxn];
int lowbit(int x)
{
return (x&(-x));
}
int query(int x)
{
int ans=0;
while(x>0)
{
ans=max(ans,tree[x]);
x-=lowbit(x);
}
return ans;
}
void update(int x,int num)
{
while(x<maxn)
{
tree[x]=max(tree[x],num);
x+=lowbit(x);
}
}
int main()
{
int c,d;
int x,y;
char s[5];
while(~scanf("%d%d%d",&n,&c,&d))
{
int cnt1=0,cnt2=0;
int ans1=0,ans2=0;
for(int i=0;i<n;i++)
{
scanf("%d%d%s",&x,&y,s);
if(s[0]=='C')
{
if(y<=c)
{
a[cnt1].b=x;
a[cnt1++].p=y;
ans1=max(ans1,x);
}
}
else if(s[0]=='D')
{
if(y<=d)
{
b[cnt2].b=x;
b[cnt2++].p=y;
ans2=max(ans2,x);
}
}
}
int ans=0;
if(ans1>0&&ans2>0) //各取一个最大值求和
ans=ans1+ans2;
mst(tree,0);
for(int i=0;i<cnt1;i++)
{
int cur=query(c-a[i].p); //查找花费在c-a[i].p内beauty的最大值
if(cur>0)
ans=max(ans,a[i].b+cur);
update(a[i].p,a[i].b); //放入该点的信息
}
mst(tree,0);
for(int i=0;i<cnt2;i++)
{
int cur=query(d-b[i].p);
if(cur>0)
ans=max(ans,b[i].b+cur);
update(b[i].p,b[i].b);
}
printf("%d\n",ans);
}
return 0;
}