Codeforces-799C-Fountains(分类讨论+线段树)

time limit per test
2 seconds
memory limit per test
256 megabytes

 Description

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 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代表接下来的n个喷泉,你拥有的种类为c的钱,和种类为d的钱的数量(两种钱不可互换)接下来每行给出一个喷泉,格式:美观值,花费,花费种类,你现在想要买两个喷泉,问你最大的美观值是多少,如果买不到2个喷泉输出0.

先分类讨论:1.买一个C类和D类,这个最简单,分开排序后挑就完事了。2.买2个C类。3.买两个D类

其中2和3相似,说一下2就好了。

买两个C类,那么我们先确定一个点i,那么在i之前一定有一个点j会满足cost[j]+cost[i]<=c。然后我们更新美观值。

至于维护cost,我们可以使用线段树,以花费为坐标,val为值,那么每个点在加入线段树之前我们先使c-该花费,然后在1到此区间内找到最大的val,最后将该点加入线段树。也就说我们先假设点i已经被买了,那么我们还能买什么使得总美观值最大。

以下是AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lc rt<<1
#define rc rt<<1|1

const int mac=1e5+10;
const int ends=1e5;

struct node
{
    int val,cost,tp;
    bool operator <(const node&a)const{
        return val>a.val;
    }
}a1[mac],a2[mac];
int tree1[mac<<2],tree2[mac<<2];

void update(int l,int r,int rt,int pos,int val,int tp)
{
    if (l==r){
        if (!tp) tree1[rt]=val;
        else tree2[rt]=val;
        return;
    }
    int mid=(l+r)>>1;
    if (mid>=pos) update(lson,pos,val,tp);
    else update(rson,pos,val,tp);
    tree1[rt]=max(tree1[lc],tree1[rc]);
    tree2[rt]=max(tree2[lc],tree2[rc]);
}

int query(int l,int r,int rt,int L,int R,int tp)
{
    int ans=0;
    if (l>=L && r<=R){
        if (!tp) return tree1[rt];
        else return tree2[rt];
    }
    int mid=(l+r)>>1;
    if (mid>=L) ans=max(ans,query(lson,L,R,tp));
    if (mid<R) ans=max(ans,query(rson,L,R,tp));
    return ans; 
}

int main()
{
    int n,c,d;
    scanf ("%d%d%d",&n,&c,&d);
    char s[5];
    int cnt1=0,cnt2=0;
    for (int i=1; i<=n; i++){
        int x,y;
        scanf ("%d%d%s",&x,&y,s);
        if (s[0]=='C') a1[++cnt1]=node{x,y,0};
        else a2[++cnt2]=node{x,y,1};
    }
    sort(a1+1,a1+1+cnt1);
    sort(a2+1,a2+1+cnt2);
    int mk1,mk2,mk3,ans1,ans2,ans3;
    mk1=mk2=mk3=ans1=ans2=ans3=0;
    for (int i=1; i<=cnt1; i++) if (a1[i].cost<=c) {ans1+=a1[i].val;mk1++;break;}
    for (int i=1; i<=cnt2; i++) if (a2[i].cost<=d) {ans1+=a2[i].val;mk1++;break;}
    if (mk1<2) ans1=0;
    for (int i=1; i<=cnt1; i++){
        int mx=a1[i].val;
        if (c<=a1[i].cost) continue;
        int p=query(1,ends,1,1,c-a1[i].cost,0);
        update(1,ends,1,a1[i].cost,a1[i].val,0);
        if (!p) continue;
        else ans2=max(ans2,mx+p);
    }
    for (int i=1; i<=cnt2; i++){
        int mx=a2[i].val;
        if (d<=a2[i].cost) continue;
        int p=query(1,ends,1,1,d-a2[i].cost,1);
        update(1,ends,1,a2[i].cost,a2[i].val,1);
        if (!p) continue;
        else ans3=max(ans3,mx+p);
    }
    printf("%d
",max(ans1,max(ans2,ans3)));
    return 0;
}
路漫漫兮
原文地址:https://www.cnblogs.com/lonely-wind-/p/12196431.html