C.Fountains(Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)+线段树+RMQ)
题目链接: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)的更多相关文章
- 树状数组 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 ...
- 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 ...
- 【预处理】【分类讨论】Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains
分几种情况讨论: (1)仅用C或D买两个 ①买两个代价相同的(实际不同)(排个序) ②买两个代价不同的(因为买两个代价相同的情况已经考虑过了,所以此时对于同一个代价,只需要保存美丽度最高的喷泉即可)( ...
- 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 ...
- 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 ...
- Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) E - Aquarium decoration 贪心 + 平衡树
E - Aquarium decoration 枚举两个人都喜欢的个数,就能得到单个喜欢的个数,然后用平衡树维护前k大的和. #include<bits/stdc++.h> #define ...
- 【动态规划】【滚动数组】【搜索】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]]= ...
- 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 ...
- 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 ...
随机推荐
- 在linux下编译线程程序undefined reference to `pthread_create'
由于是Linux新手,所以现在才开始接触线程编程,照着GUN/Linux编程指南中的一个例子输入编译,结果出现如下错误:undefined reference to 'pthread_create'u ...
- phaser入手
做phaser小程序,必须先把环境弄好 发现怎么导入都无济于事. 最后决定亲自操刀,在原代码中,引入全局变量.
- Hibernate 中 load() 和 get() 的区别
get 和 load 方式都是是根据 id 取得一个记录.下边详细说一下 get 和 load 的不同,因为有些时候为了对比也会把 find 加进来. 1.从返回结果上对比: load 方式检索不到的 ...
- Hibernate 中一级缓存和快照区的理解
刚刚开始的时候觉得这个快照区很难理解,在网上看了很多博客之后,开始明白了.我是结合 ADO.NET 理解的,在ADO.NET 中有一个类, 叫 SqlCommandBuilder,在我看来,他就是 A ...
- vue & $data & data
vue & $data & data vm.a === vm.$data.a https://vuejs.org/v2/api/#data https://flaviocopes.co ...
- vue.js+vue-router+webpack keep-alive用法
本文是机遇 提纲: 现有需求 各个解决方案的优缺点 相关的问题延伸 keep-alive使用详解 现有需求 每个项目中都存在许多列表数据展示页面,而且通常包含一些筛选条件以及分页. 并 ...
- 洛谷 P2146 [NOI2015]软件包管理器
真没有想到,这竟然会是一道NOI的原题,听RQY说,这套题是北大出的,北大脑抽认为树剖很难... 只恨没有早学几年OI,只A这一道题也可以出去吹自己一A了NOI原题啊 好了,梦该醒了,我们来看题 以后 ...
- Codeforces Round #362(Div1) D Legen...(AC自动机+矩阵快速幂)
题目大意: 给定一些开心串,每个串有一个开心值,构造一个串,每包含一次开心串就会获得一个开心值,求最大获得多少开心值. 题解: 首先先建立AC自动机.(建立fail指针的时候,对val要进行累加) 然 ...
- 关于upper、lower bound 的探讨
lower_bound(A, A+n, x) - A 返回第一个大于等于x的数的下标 lower_bound(A, A+n, x) - A - 1 返回最后一个小于x的数的下标 upper_boun ...
- 【刷题】BZOJ 1951 [Sdoi2010]古代猪文
Description "在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心--" --选自猪王国民歌 很久 ...