Fountains 【CodeForces - 799C】【线段树】

题目链接


  这道题一开始让我懵了好一会,原来是要恰好两座喷泉才可以计算最优解的说。

  有如下N个方案,每个方案有那么些值,譬如说按照输入顺序分别是:美观度、价格、货币的种类。我们要求的是这样子的情况,我们有两种货币各自C、D个,求的是以手上的钱,最多的美观度的和是多少?

  那么,不如就直接去求,因为货币数量最多达到1e5,所以,我们可以直接用线段树的方式来解这道题,区间上是该种货币的价格所对应的第一大、第二大的美观度,如果出现等价值不等美观度的时候,需要有这样的考虑。

  然后,直接O(N*logN)的去搜索所有的答案,直接枚举即可。但是枚举的时候,记得提前删除自己的情况。


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e5 + 7;
const int _UP = 1e5;
int N, C, D;
struct fountain
{
    int cost, beauty;
    fountain(int a=0, int b=0):cost(a), beauty(b) {}
}a[maxN], b[maxN];
int cnt, tot;
struct node
{
    int first, second;
    node(int a=0, int b=0):first(a), second(b) {}
}ct[maxN<<2], dt[maxN<<2];
void pushup(node *tree, int rt) { tree[rt].first = max(tree[lsn].first, tree[rsn].first); }
void update(node *tree, int rt, int l, int r, int pos, int val) //pos需要的钱,val美观度
{
    if(l == r)
    {
        if(tree[rt].first)
        {
            if(val >= tree[rt].first) tree[rt] = node(val, tree[rt].first);
            else if(val > tree[rt].second) tree[rt].second = val;
        }
        else tree[rt].first = val;
        return;
    }
    int mid = HalF;
    if(pos <= mid) update(tree, Lson, pos, val);
    else update(tree, Rson, pos, val);
    pushup(tree, rt);
}
void Delet(node *tree, int rt, int l, int r, int pos, int val)
{
    if(l == r)
    {
        if(tree[rt].first == val) tree[rt].first = tree[rt].second;
        return;
    }
    int mid = HalF;
    if(pos <= mid) Delet(tree, Lson, pos, val);
    else Delet(tree, Rson, pos, val);
    pushup(tree, rt);
}
int query(node *tree, int rt, int l, int r, int ql, int qr)
{
    if(ql <= l && qr >= r) return tree[rt].first;
    int mid = HalF;
    if(qr <= mid) return query(tree, QL);
    else if(ql > mid) return query(tree, QR);
    else return max(query(tree, QL), query(tree, QR));
}
char op[3];
int main()
{
    cnt = tot = 0;
    scanf("%d%d%d", &N, &C, &D);
    for(int i=1, e1, e2; i<=N; i++)
    {
        scanf("%d%d%s", &e1, &e2, op);
        if(op[0] == 'C')
        {
            a[++cnt] = fountain(e2, e1);
            update(ct, 1, 1, _UP, e2, e1);
        }
        else
        {
            b[++tot] = fountain(e2, e1);
            update(dt, 1, 1, _UP, e2, e1);
        }
    }
    int ans = 0;
    for(int i=1; i<=cnt; i++) //取两个一样的货币的——硬币
    {
        if(a[i].cost >= C) continue;
        Delet(ct, 1, 1, _UP, a[i].cost, a[i].beauty);
        int tmp = query(ct, 1, 1, _UP, 1, C - a[i].cost);
        update(ct, 1, 1, _UP, a[i].cost, a[i].beauty);
        if(tmp) ans = max(ans, tmp + a[i].beauty);
    }
    for(int i=1; i<=tot; i++)
    {
        if(b[i].cost >= D) continue;
        Delet(dt, 1, 1, _UP, b[i].cost, b[i].beauty);
        int tmp = query(dt, 1, 1, _UP, 1, D - b[i].cost);
        update(dt, 1, 1, _UP, b[i].cost, b[i].beauty);
        if(tmp) ans = max(ans, tmp + b[i].beauty);
    }
    int tmp1 = query(ct, 1, 1, _UP, 1, C), tmp2 = query(dt, 1, 1, _UP, 1, D);   //取两个不一样的
    if(tmp1 && tmp2) ans = max(ans, tmp1 + tmp2);
    printf("%d\n", ans);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值