Codeforces799C. 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.

遇事不决 先膜来神

来神还是强啊

这题如果你在用C和D各买一个就很简单,sort之后挑一个能买的最贵的。
然后还有两种情况就是C里面买两个 或者D里面买两个,这怎么办呢。由于是挑两个,那么如果你挑了一个之后 另一个能挑的区间就确定了,那么 区间查询。。。最大值。。。嗯 。。线段树。。
这里还有个细节处理,是边更新边查询,而不是一次性更新完然后便利查询,这样可以防止自己访问自己2333自己体会一下

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<vector>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<string>
#include<stack>
#include<map>
using namespace std;

//thanks to pyf ...

#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
typedef pair<int, int> PII;
typedef long long ll;

const int N = 1e6 + 5;

struct
{
    int l, r;
    int Max;
} t[N * 4];
ll a[N];
ll cost_a[N];
int cnt_a[N];
int cnt_b[N];
ll b[N];
ll cost_b[N];
void build(int l, int r, int step)
{
    t[step].l = l;
    t[step].r = r;
    t[step].Max = -1;
    if (l == r)
        return;
    int mid = (l + r) / 2;
    build(l, mid, step * 2);
    build(mid + 1, r, step * 2 + 1);
}
void push_up(int step)
{
    t[step].Max = max(t[step * 2].Max, t[step * 2 + 1].Max);

}
void update(int x, int val, int step)
{
    if (t[step].l == t[step].r)
    {
        t[step].Max = max(t[step].Max , val);
        return ;
    }
    int mid = (t[step].l + t[step].r) / 2;
    if (x <= mid)
        update(x, val, step * 2);
    else
        update(x, val, step * 2 + 1);
    push_up(step);
}
int query(int l, int r, int step)
{
    if (t[step].l == l && t[step].r == r)
        return t[step].Max;
    int mid = (t[step].l + t[step].r) / 2;
    if (r <= mid)
        return query(l, r, step * 2);
    else if (l > mid)
        return query(l, r, step * 2 + 1);
    else
        return max(query(l, mid, step * 2), query(mid + 1, r, step * 2 + 1));
}
int main()
{
    int n, c, d;
    while (cin >> n >> c >> d)
    {
        CLR(cnt_a, 0);
        CLR(cnt_b, 0);
        int cnt_c = 0;
        int cnt_d = 0;
        int MaxC = 0;
        for (int i = 0; i < n; i++)
        {
            int x, y;
            string s;
            cin >> x >> y >> s;
            MaxC = max(MaxC, y);
            if (s[0] == 'C')
                a[cnt_c] = x, cost_a[cnt_c++] = y, cnt_a[x]++;
            else
                b[cnt_d] = x, cost_b[cnt_d++] = y, cnt_b[x]++;
        }
        ll ans = -1;
        ll res1 = -1, res2 = -1;
        for (int i = 0; i < cnt_c; i++)
        {
            if (cost_a[i] <= c)
                res1 = max(res1, a[i]);
        }
        for (int i = 0; i < cnt_d; i++)
        {
            if (cost_b[i] <= d)
                res2 = max(res2, b[i]);
        }
        if (res1 != -1 && res2 != -1)
            ans = res1 + res2;
        MaxC = max(MaxC,max(c,d));
        build(1, MaxC+10, 1);
        for (int i = 0; i < cnt_c; i++)
        {
            ll res = -1;
            if (c > cost_a[i])
                res = query(1, c - cost_a[i] , 1);
            update(cost_a[i], a[i], 1);
            if (res == -1)
                continue;
            ans = max(ans, res + a[i]);
        }
        build(1, MaxC + 10, 1);
        for (int i = 0; i < cnt_d; i++)
        {
            ll res = -1;
            if (d > cost_b[i])
                res = query(1, d - cost_b[i] , 1);
            update(cost_b[i], b[i], 1);
            if (res == -1)
                continue;
            ans = max(ans, res + b[i]);
        }
        if (ans == -1)
            cout << 0 << endl;
        else
            cout << ans << endl;
    }
    return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值