codeforces Round #413 Div. 1 + Div. 2 C Fountains

来源:互联网 发布:weka apriori算法 编辑:程序博客网 时间:2024/05/18 02:41

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 0000 ≤ 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 610 8 C4 3 C5 6 D
output
9
input
2 4 52 5 C2 1 D
output
0
input
3 10 105 5 C5 5 C10 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.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

二叉搜索树~

看起来好简单……可是注意的地方好多……说起来这还是我第一次写二叉搜索树啊……调到吐血……

(考试的时候写了个奇怪的贪心算法100多行,结果调不出来了……所以代码一定要精简啊)

总共只有三种情况:C类和D类各拿一个,两个都是C类,两个都是D类。

在输入的时候就处理一下,把没法买的去掉,刚好只能买一个的记录一下价值也去掉。

第一种情况可以在输入的时候直接记录一下,如果两类都有,就更新答案。

第二类和第三类一样,只要清空一下树就可以了。

我们把所有能取到的物品排序,花费是第一关键字,价值是第二关键字。那么每次在二叉树中搜索能与它匹配的最大价值,更新答案,再把它放到二叉树中。

注意:

1.花费大的不一定价值大;

2.不能合并同花费物品,因为同花费物品也可能组成一对;

3.要去掉不能与任何物品组队的物品;

4.二叉树中搜索的是[1,x]这个区间;

5.要先搜索再加入,这样可以保证不会与自己组队。


#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;#define N 100005int n,c,d,x,y,a[N<<2],tot1,tot2,max1,max2,maxx;bool b1,b2;char s[2];struct node{int cos,val;}numa[N],numb[N];bool operator < (node u,node v){return u.cos==v.cos ? u.val<v.val:u.cos<v.cos;}int read(){int x=0,f=1;char ch=getchar();while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}while(ch>='0' && ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}void add(int l,int r,int k,int u,int v){if(l==r){a[k]=max(a[k],v);return;}int mid=l+r>>1;if(u<=mid) add(l,mid,k<<1,u,v);else add(mid+1,r,k<<1|1,u,v);a[k]=max(a[k<<1],a[k<<1|1]);}int findd(int l,int r,int k,int u){if(r<=u) return a[k];int mid=l+r>>1;if(u<=mid) return findd(l,mid,k<<1,u);return max(findd(l,mid,k<<1,u),findd(mid+1,r,k<<1|1,u));}int main(){n=read();c=read();d=read();for(int i=1;i<=n;i++){x=read();y=read();scanf("%s",s);if(s[0]=='C' && y<c) numa[++tot1]=(node){y,x},max1=max(max1,x),b1=1;else if(s[0]=='D' && y<d) numb[++tot2]=(node){y,x},max2=max(max2,x),b2=1;else if(s[0]=='C' && y==c) max1=max(max1,x),b1=1;else if(s[0]=='D' && y==d) max2=max(max2,x),b2=1;}maxx=b1*b2 ? max1+max2:0;if(tot1>1){sort(numa+1,numa+tot1+1);for(int i=1;i<=tot1;i++){x=c-numa[i].cos;y=numa[i].val;int z=findd(1,100000,1,x);maxx=z ? max(maxx,y+z):maxx;add(1,100000,1,numa[i].cos,y);}}if(tot2>1){if(tot1>1) memset(a,0,sizeof(a));sort(numb+1,numb+tot2+1);for(int i=1;i<=tot2;i++){x=d-numb[i].cos;y=numb[i].val;int z=findd(1,100000,1,x);maxx=z ? max(maxx,y+z):maxx;add(1,100000,1,numb[i].cos,y);}}printf("%d\n",maxx);return 0;}


1 0
原创粉丝点击