题目链接:http://codeforces.com/contest/799/problem/C

题目:

题意:

  给你n种喷泉的价格和漂亮值,这n种喷泉题目指定用钻石或现金支付(分别用D和C表示),C和D之间不能相互转换。你现在需要修建两个喷泉,给你硬币数和现金数,问你怎样才能使修建的两个喷泉的总漂亮值最大。

思路:

  易知,要修建的两个喷泉如果一个是用钻石支付,另一个用现金支付,那么只需找到小于给的钻石和现金的上限的漂亮值最大的两个温泉相加,此处遍历一边即可。对于这两个温泉都用同一种支付方式的情况(拿钻石支付的温泉来举例),我们可以用线段树来维护价格小于(C - 当前价格)的那些喷泉中漂亮值的最大值是多少(区间查询),当这个最大值是0时代表前面没有满足条件的喷泉,如果不是0,那么我们就看是否需要更新答案,最后我们再将当前的这个喷泉更新到线段树中(单点更新)。因为我们知道当x1+x2是最大值,那么x2+x1也一定是最大值,因此我们不排序直接这样进行查询更新是非常合理的。

代码实现如下:

 #include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;;
typedef pair<int, int> pii;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define bug printf("*********\n");
#define FIN freopen("D://code//in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = ;
const int maxn = 1e5 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f; int n, c, d, bea, cost, mx1, mx2, t1, t2;
char op[]; struct node {
int bea, cost;
node(int bea = , int cost = ) : bea(bea), cost(cost) {}
}num1[maxn], num2[maxn]; struct tree {
int l, r, mx;
}segtree[maxn*]; void push_up(int i) {
segtree[i].mx = max(segtree[i*].mx, segtree[i*+].mx);
} void build(int i, int l, int r) {
segtree[i].l = l, segtree[i].r = r;
if(l == r) {
segtree[i].mx = ;
return;
}
int mid = (l + r) >> ;
build(i * , l, mid);
build(i * + , mid + , r);
push_up(i);
} void update(int i, int pos, int val) {
if(segtree[i].l == pos && segtree[i].r == pos) {
segtree[i].mx = max(segtree[i].mx, val);
return;
}
int mid = (segtree[i].l + segtree[i].r) >> ;
if(pos <= mid) update(i*, pos, val);
else update(i*+, pos, val);
push_up(i);
} int query(int i, int l, int r) {
if(segtree[i].l == l && segtree[i].r == r) {
return segtree[i].mx;
}
int mid = (segtree[i].l + segtree[i].r) >> ;
if(r <= mid) return query(i*, l, r);
else if(l > mid) return query(i*+, l, r);
else return max(query(i*, l, mid), query(i*+, mid + , r));
} int main() {
scanf("%d%d%d", &n, &c, &d);
mx1 = -, mx2 = -;
for(int i = ; i <= n; i++) {
scanf("%d%d%s", &bea, &cost, op);
if(op[] == 'C') {
if(cost <= c && bea > mx1) {
mx1 = bea;
}
num1[++t1] = node(bea, cost);
} else {
if(cost <= d && bea > mx2) {
mx2 = bea;
}
num2[++t2] = node(bea, cost);
}
}
int ans = ;
build(, , maxn); //因为C-num[i].cost可能等于0,所以线段树的区间需要从0开始
for(int i = ; i <= t1; i++) {
if(c >= num1[i].cost) {
int t = query(, , c - num1[i].cost);
if(t != ) {
ans = max(ans, t + num1[i].bea);
}
}
update(, num1[i].cost, num1[i].bea);
}
build(, , maxn);
for(int i = ; i <= t2; i++) {
if(d >= num2[i].cost) {
int t = query(, , d - num2[i].cost);
if(t != ) {
ans = max(ans, t + num2[i].bea);
}
}
update(, num2[i].cost, num2[i].bea);
}
if(mx1 != - && mx2 != -) { //一个用钻石支付,一个用现金支付
ans = max(ans, mx1 + mx2);
}
printf("%d\n", ans);
return ;
}

C.Fountains(Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)+线段树+RMQ)的更多相关文章

  1. 树状数组 Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains

    C. Fountains time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  2. Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains 【树状数组维护区间最大值】

    题目传送门:http://codeforces.com/contest/799/problem/C C. Fountains time limit per test 2 seconds memory ...

  3. 【预处理】【分类讨论】Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains

    分几种情况讨论: (1)仅用C或D买两个 ①买两个代价相同的(实际不同)(排个序) ②买两个代价不同的(因为买两个代价相同的情况已经考虑过了,所以此时对于同一个代价,只需要保存美丽度最高的喷泉即可)( ...

  4. Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)(A.暴力,B.优先队列,C.dp乱搞)

    A. Carrot Cakes time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

  5. Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) 一夜回到小学生

    我从来没想过自己可以被支配的这么惨,大神讲这个场不容易掉分的啊 A. Carrot Cakes time limit per test 1 second memory limit per test 2 ...

  6. Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) E - Aquarium decoration 贪心 + 平衡树

    E - Aquarium decoration 枚举两个人都喜欢的个数,就能得到单个喜欢的个数,然后用平衡树维护前k大的和. #include<bits/stdc++.h> #define ...

  7. 【动态规划】【滚动数组】【搜索】Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) D. Field expansion

    显然将扩张按从大到小排序之后,只有不超过前34个有效. d[i][j]表示使用前i个扩张,当length为j时,所能得到的最大的width是多少. 然后用二重循环更新即可, d[i][j*A[i]]= ...

  8. Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) D. Field expansion

    D. Field expansion time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  9. Educational Codeforces Round 6 E. New Year Tree dfs+线段树

    题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...

随机推荐

  1. String、StringBuilder与StringBuffer的区别

    1.String类是public.final修饰的. 在Java中,被final修饰的类是不允许被继承的,并且String它的成员方法都默认为final方法. 查看源码得知,String类其实是通过c ...

  2. 6/3 sprint2 看板和燃尽图的更新

  3. (六)hadoop系列之__hadoop分布式集群环境搭建

    配置hadoop(master,slave1,slave2) 说明: NameNode: master DataNode: slave1,slave2 ------------------------ ...

  4. linux下c/c++的文件操作

    opendir,readdir,closedir, stat()查询文件状态 open(), O_TRUNC这个Flag会把打开的文件清零... 文件锁:fcntl, F_GETLK , F_SETL ...

  5. java多线程之CAS原理

    前言 在Java并发包中有这样一个包,java.util.concurrent.atomic,该包是对Java部分数据类型的原子封装,在原有数据类型的基础上,提供了原子性的操作方法,保证了线程安全.下 ...

  6. 两个float 怎么比较大小

    转自:http://blog.csdn.net/mydriverc2/article/details/49888947 float 类型不能比较相等或不等,但可以比较>,<,>=,& ...

  7. ZOJ3466-The Hive II

    题意 有一个六边形格子,共 \(n\) 行,每行有 8 个位置,有一些格子不能走.求用一些环覆盖所有可走格子的方案数.\(n\le 10\) . 分析 插头dp,只不过是六边形上的,分奇数列和偶数列讨 ...

  8. HDU5266-pog loves szh III

    题目 给出一棵\(n\)个点的树,从1到\(n\)编号,\(m\)次询问\({LCA} _{v\in[L,R]}\). \(n,m\le 3\times 10^5​\) 分析 我的做法是直接对LCA进 ...

  9. 秒杀多线程第七篇 经典线程同步 互斥量Mutex(续)

    java使用Synchronized关键字实现互斥,而同时有Lock支持. 这两个的效果是等同的,Synchronized性能的起伏较大,而lock比较收敛. 为了代码的可读性,Synchronize ...

  10. BZOJ3566 SHOI2014概率充电器(动态规划+概率期望)

    设f[i]为i在子树内不与充电点连通的概率.则f[i]=(1-pi)·∏(1-qk+qk·f[k]). 然后从父亲更新答案.则f[i]=f[i]·(1-qfa+qfa*f[fa]/(1-qfa+qfa ...