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.
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.
Print the maximum total beauty of exactly two fountains Arkady can build. If he can't build two fountains, print 0.
3 7 6 10 8 C 4 3 C 5 6 D
9
2 4 5 2 5 C 2 1 D
0
3 10 10 5 5 C 5 5 C 10 11 D
10
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去支付,每个物品只能购买一次。
现在要买两个物品,问获得的最大价值是多少。
思路:
我们将购买的情况分成三种情况:
首先我们将C购买的物品和D购买的物品分成两类。然后进行处理。
①1个用C购买,一个用D购买。
那么对应找到用C可以购买的最高价值的物品,以及用D可以购买的最高价值的物品累加即可。
②两个物品都用C购买。
那么问题还要分成两类。
要么两个物品购买的都是同一花费,要么两个物品购买的不是同一花费。
同一花费的排个序随便维护一下即可,不同花费的话,我们用线段树维护。
线段树中保存在位子i中表示的是花费为i的最大价值物品的价值。
那么对应我们O(n)枚举出来一个要购买的物品C ,假设其价值为Ci,那么我们要在线段树中查询【1,C-ci】的最大值。
为什么一定要分成相同和不相同花费去处理呢?因为每个物品只能买一次,每一次我们枚举出来的C物品都要从树上去除掉,所以这里我们不能让树上任意一个位子有两个值。
所以我们要分成两种情况,一种是相同花费,一种是不相同花费。
③两个物品都用D购买,同C思路。
Ac代码(写的有点挫将就看吧):
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
struct node
{
int val,cost;
} c[100500],d[100500];
int tree[1000050*5];
int use[100500];
int n,C,D,cnt,tot;
void pushup(int rt)
{
tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
}
int Query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return tree[rt];
}
else
{
int m=(l+r)>>1;
int ans=0;
if(L<=m)
{
ans=max(Query(L,R,lson),ans);
}
if(m<R)
{
ans=max(Query(L,R,rson),ans);
}
return ans;
}
}
void build( int l ,int r , int rt )
{
if( l == r )
{
tree[rt]=0;
return ;
}
else
{
int m = (l+r)>>1 ;
build(lson) ;
build(rson) ;
pushup(rt) ;
}
}
void update(int p,int c,int l,int r,int rt)//p阵营c数据.
{
if(l==r)
{
tree[rt]+=c;
}
else
{
int m=(l+r)>>1;
if(p<=m) update(p,c,lson);
else update(p,c,rson);
pushup(rt);
}
}
int cmp(node a,node b)
{
return a.val>b.val;
}
int cmp2(node a,node b)
{
if(a.cost==b.cost)return a.val>b.val;
return a.cost<b.cost;
}
int Slove()
{
int output=0;
sort(c,c+cnt,cmp);
sort(d,d+tot,cmp);
int flag=0;
for(int i=0; i<cnt; i++)
{
if(c[i].cost<=C)
{
flag=1;
output+=c[i].val;
break;
}
}
if(flag==0)return 0;
flag=0;
for(int i=0; i<tot; i++)
{
if(d[i].cost<=D)
{
flag=1;
output+=d[i].val;
break;
}
}
if(flag==0)return 0;
else
return output;
}
int Slove2()
{
int output=0;
memset(use,0,sizeof(use));
for(int i=0;i<cnt;i++)
{
use[c[i].cost]=max(use[c[i].cost],c[i].val);
}
sort(c,c+cnt,cmp2);
for(int i=1;i<cnt;i++)
{
if(c[i].cost==c[i-1].cost&&c[i].cost*2<=C)
{
output=max(output,c[i].val+c[i-1].val);
}
}
build(1,100400,1);
for(int i=1;i<=100400;i++)
{
update(i,use[i],1,100400,1);
}
for(int i=1;i<C;i++)
{
if(use[i]==0)continue;
update(i,-use[i],1,100400,1);
int tmp=Query(1,C-i,1,100400,1);
if(tmp>0)
{
output=max(tmp+use[i],output);
}
update(i,use[i],1,100400,1);
}
return output;
}
int Slove3()
{
int output=0;
memset(use,0,sizeof(use));
for(int i=0;i<tot;i++)
{
use[d[i].cost]=max(use[d[i].cost],d[i].val);
}
sort(d,d+tot,cmp2);
for(int i=1;i<tot;i++)
{
if(d[i].cost==d[i-1].cost&&d[i].cost*2<=D)
{
output=max(output,d[i].val+d[i-1].val);
}
}
build(1,100400,1);
for(int i=1;i<=100400;i++)
{
update(i,use[i],1,100400,1);
}
for(int i=1;i<D;i++)
{
if(use[i]==0)continue;
update(i,-use[i],1,100400,1);
int tmp=Query(1,D-i,1,100400,1);
if(tmp>0)
{
output=max(tmp+use[i],output);
}
update(i,use[i],1,100400,1);
}
return output;
}
int main()
{
while(~scanf("%d%d%d",&n,&C,&D))
{
cnt=0;
tot=0;
for(int i=0; i<n; i++)
{
int val,cost;
char tmp[15];
scanf("%d%d%s",&val,&cost,tmp);
if(tmp[0]=='C')
{
c[cnt].val=val,c[cnt].cost=cost;
cnt++;
}
if(tmp[0]=='D')
{
d[tot].val=val,d[tot].cost=cost;
tot++;
}
}
int output=0;
output=max(Slove(),output);
output=max(Slove2(),output);
output=max(Slove3(),output);
printf("%d\n",output);
}
}