Codeforces Round #413 Div. 2 C. Fountains

来源:互联网 发布:c#课本编程题及答案 编辑:程序博客网 时间:2024/04/30 14:21

C. Fountains
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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货币。给出每个商品的贡献和代价以及能用哪种货币购买。问你能买到两件商品贡献和最大为多少。
题解:枚举一个商品的同时,找到剩余钱所能购买的最大贡献是多少,维护最大值。
代码:

#include<bits/stdc++.h>using namespace std;const int N=1e6;int C[N<<2],D[N<<2];int n,c,d,b,p;string s;void pushup(int rt,int *t){    t[rt]=max(t[rt<<1],t[rt<<1|1]);}void update(int l,int r,int pos,int val,int rt,int *t){    if(l==r)    {        t[rt]=max(t[rt],val);        return ;    }    int mid=(l+r)>>1;    if(pos<=mid) update(l,mid,pos,val,rt<<1,t);    else update(mid+1,r,pos,val,rt<<1|1,t);    pushup(rt,t);}int query(int l,int r,int ll,int rr,int rt,int *t){    if(ll>rr) return 0;    if(ll<=l&&rr>=r) return t[rt];    int res=0;    int mid=(r+l)>>1;    if(ll<=mid) res=max(res,query(l,mid,ll,rr,rt<<1,t));    if(rr>mid)  res=max(res,query(mid+1,r,ll,rr,rt<<1|1,t));    return res;}int main(){    cin>>n>>c>>d;    int ans=0;    for(int i=1;i<=n;i++)    {        cin>>b>>p>>s;        int sum;        if(s=="C")        {            sum=query(1,N,1,d,1,D);            if(p>c) continue;            sum=max(sum,query(1,N,1,c-p,1,C));            update(1,N,p,b,1,C);        }        else        {            sum=query(1,N,1,c,1,C);            if(p>d) continue;            sum=max(sum,query(1,N,1,d-p,1,D));            update(1,N,p,b,1,D);        }        if(sum) ans=max(ans,sum+b);    }    cout<<ans<<endl;}
0 0