CodeforcesPlayrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)C题C. Fountains

来源:互联网 发布:为什么要学linux 编辑:程序博客网 时间:2024/05/21 07:45

C. Fountains

time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard 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.

#include<bits/stdc++.h>using namespace std;int cmax,dmax;int n,cc,dd,b,p;string type;int val[3][2000001],l[3][2000001],r[3][2000001],ans;void mak(int ty,int ll,int rr,int no){    int mid=(ll+rr)/2;    l[ty][no]=ll;r[ty][no]=rr;if(ll==rr) return;    mak(ty,ll,mid,no*2);mak(ty,mid+1,rr,no*2+1);}void init(){    scanf("%d%d%d",&n,&cc,&dd);    mak(1,1,100000,1);mak(2,1,100000,1);}int get(int ty,int ll,int rr,int no){    int mid=(l[ty][no]+r[ty][no])/2;    if(ll==l[ty][no]&&rr==r[ty][no]) return val[ty][no];    if(rr<=mid) return get(ty,ll,rr,no*2);    if(ll>mid) return get(ty,ll,rr,no*2+1);    return max(get(ty,ll,mid,no*2),get(ty,mid+1,rr,no*2+1));}void add(int ty,int b,int p,int no){    val[ty][no]=max(val[ty][no],b);    if(l[ty][no]==r[ty][no]) return;    int mid=(l[ty][no]+r[ty][no])/2;    if(mid>=p) add(ty,b,p,no*2);    else add(ty,b,p,no*2+1);}int  main(){    init();    int lol;    for(int i=1;i<=n;i++)    {        cin>>b>>p>>type;lol=0;        if(type=="C")        {            if(p>cc) continue;cmax=max(b,cmax);if(p==cc)continue;            int lol=get(1,1,cc-p,1);            if(lol) ans=max(ans,lol+b);            add(1,b,p,1);        }        else        {            lol=0;            if(p>dd) continue;dmax=max(b,dmax);if(p==dd)continue;            int lol=get(2,1,dd-p,1);            if(lol) ans=max(ans,lol+b);            add(2,b,p,1);        }    }    if(dmax&&cmax) ans=max(ans,dmax+cmax);    printf("%d",ans);}

三种情况
1、一个钻石买的,一个coin买的
2、两个钻石买的
3、两个coin买的
我们用线段树维护区间最小值(因为该算法是在线的)
如果某一个没有被查找到,我们就不更新ans。
两颗线段树 ,每次取加入该元素前的线段树来取最大值,后面的会覆盖前面的所以最优解一定存在。

2 0
原创粉丝点击