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;
}
### Codeforces Div.2 比赛难度介绍 Codeforces Div.2 比赛主要面向的是具有基础编程技能到中级水平的选手。这类比赛通常吸引了大量来自全球不同背景的参赛者,包括大学生、高中生以及一些专业人士。 #### 参加资格 为了参加 Div.2 比赛,选手的评级应不超过 2099 分[^1]。这意味着该级别的竞赛适合那些已经掌握了一定算法知识并能熟练运用至少一种编程语言的人群参与挑战。 #### 题目设置 每场 Div.2 比赛一般会提供五至七道题目,在某些特殊情况下可能会更多或更少。这些题目按照预计解决难度递增排列: - **简单题(A, B 类型)**: 主要测试基本的数据结构操作和常见算法的应用能力;例如数组处理、字符串匹配等。 - **中等偏难题(C, D 类型)**: 开始涉及较为复杂的逻辑推理能力和特定领域内的高级技巧;比如图论中的最短路径计算或是动态规划入门应用实例。 - **高难度题(E及以上类型)**: 对于这些问题,则更加侧重考察深入理解复杂概念的能力,并能够灵活组合多种方法来解决问题;这往往需要较强的创造力与丰富的实践经验支持。 对于新手来说,建议先专注于理解和练习前几类较容易的问题,随着经验积累和技术提升再逐步尝试更高层次的任务。 ```cpp // 示例代码展示如何判断一个数是否为偶数 #include <iostream> using namespace std; bool is_even(int num){ return num % 2 == 0; } int main(){ int number = 4; // 测试数据 if(is_even(number)){ cout << "The given number is even."; }else{ cout << "The given number is odd."; } } ```
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值