CodeForces - 799C Fountains

CodeForces - 799C Fountains

这是一个一看就知道我见过,但是我还是不会做的题,找了个题解辛辛苦苦一边理解一边抄的弄了半天,一跑……第一个测试样例就不对,气死了,抄了一个交上了,写完了这篇我再看看题的。

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<cstdio>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
const int maxn = 100010;
int a[maxn],acost[maxn],b[maxn],bcost[maxn];
struct node
{
    int Max;
    int l,r;
}t[maxn*4];
void build(int root,int l,int r)
{
    t[root].l=l;
    t[root].r=r;
    t[root].Max=-1;
    if(l==r)
        return;
    int mid=(l+r)>>1;
    build(2*root,l,mid);
    build(2*root+1,mid+1,r);
}
void update(int root,int index,int val)
{
    int l=t[root].l,r=t[root].r;
    if(l==r)
    {
        t[root].Max=max(t[root].Max,val);
        return;
    }
    int mid=(l+r)>>1;
    if(index<=mid)
    update(2*root,index,val);
    else
    update(2*root+1,index,val);
    t[root].Max=max(t[root*2].Max,t[root*2+1].Max);
}
int query(int root,int ql,int qr)
{
    int l=t[root].l;
    int r=t[root].r;

    if(l==ql&&r==qr)
    {
        return t[root].Max;
    }
    int mid=(l+r)>>1;
    if(qr<=mid)
    return query(2*root,ql,qr);
    else if(ql>mid)
    return query(2*root+1,ql,qr);
    else
    return max(query(2*root,ql,mid),query(2*root+1,mid+1,qr));
}
int main()
{
    int N,C,D;
    while(~scanf("%d%d%d",&N,&C,&D))
    {
        int acnt=0,bcnt=0;
        char ch;
        int c,w;
        int Maxc=-1;
        for(int i=0;i<N;i++)
        {
            scanf("%d%d %c",&c,&w,&ch);
            if(ch=='C')
            {
                a[acnt]=c;acost[acnt++]=w;
            }
            else if(ch=='D')
            {
                b[bcnt]=c;bcost[bcnt++]=w;
            }
            Maxc=max(w,Maxc);
        }
        int ans=-1;
        int res1=-1,res2=-1;
        for(int i=0;i<acnt;i++)
        {
            if(C>=acost[i])
            res1=max(res1,a[i]);
        }
        for(int i=0;i<bcnt;i++)
        {
            if(D>=bcost[i])
            res2=max(res2,b[i]);
        }
        if(res1!=-1&&res2!=-1)ans=res1+res2;

        Maxc=max(Maxc,max(C,D));
        build(1,1,Maxc+10);
        for(int i=0;i<acnt;i++)
        {
            int res=-1;
            if(C>acost[i])
            res=query(1,1,C-acost[i]);
            update(1,acost[i],a[i]);
            if(res!=-1)
            ans=max(ans,a[i]+res);
        }
        build(1,1,Maxc);
        for(int i=0;i<bcnt;i++)
        {
            int res=-1;
            if(D>bcost[i])
            res=query(1,1,D-bcost[i]);
            update(1,bcost[i],b[i]);
            if(res!=-1)
            ans=max(ans,b[i]+res);
        }
        if(ans!=-1)
        printf("%d\n",ans);
        else
        printf("0\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值