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

本文介绍了一种在游戏内购中优化玩家体验的方法,通过构建最优的喷泉建造方案,利用线段树算法来实现资源的最大化利用。该方案考虑了不同货币类型之间的限制,并通过实例展示了如何为玩家提供最佳的游戏内购买选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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。
两颗线段树 ,每次取加入该元素前的线段树来取最大值,后面的会覆盖前面的所以最优解一定存在。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值