Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains(树状数组)

来源:互联网 发布:软件服务外包合同范本 编辑:程序博客网 时间:2024/04/30 15:48

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

题意:要求建两个喷泉,现在有n个喷泉可以选,每一个喷泉的价格和漂亮度都已经给出,这里有两种货币,硬币和钻石,所以每一个喷泉只能用这两个货币中的一种购买。求出他能建的喷泉的方法中最大的漂亮度。


要建两个喷泉,一共就三种情况,选一个用硬币买的喷泉再选一个用钻石买的喷泉,或者选两个用硬币买的喷泉,或者选两个用钻石买的喷泉。

那么就是枚举一个喷泉,然后再用剩下的钱去买一个漂亮度最大的喷泉,这样就可以求出最大值。

那么怎么快速求出这个漂亮度最大的喷泉呢,观察发现价格的范围不是很大,所以可以利用树状数组存下价格为p的喷泉的漂亮度,那么就能快速求出价格为1~p的最大漂亮度。

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int maxn = 100000;int C[maxn+10],D[maxn+10];void add(int *tree,int k,int num){while(k<=maxn){tree[k] = max(tree[k],num);k+=k&-k;}}int read(int *tree,int k){int res=0;while(k){res = max(res,tree[k]);k-=k&-k;}return res;}int main(void){    int n,c,d,i,j;    while(scanf("%d%d%d",&n,&c,&d)==3)    {        memset(C,0,sizeof(C));        memset(D,0,sizeof(D));        int ans = 0;        for(i=1;i<=n;i++)        {            int b,p;            char t[5];            scanf("%d%d%s",&b,&p,t);            int maxn;            if(t[0] == 'C')            {                maxn = read(D,d);                if(p > c)                    continue;                maxn = max(maxn,read(C,c-p));                add(C,p,b);            }            else            {                maxn = read(C,c);                if(p > d)                    continue;                maxn = max(maxn,read(D,d-p));                add(D,p,b);            }            if(maxn)                ans = max(ans,maxn + b);        }        cout << ans << endl;    }    return 0;}


0 0
原创粉丝点击