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 ...
随机推荐
- django的第一个问题
/usr/local/lib/python2.7/dist-packages/allauth/account/utils.py in setup_user_email, line 258 /usr/l ...
- css 层加透明度后文字依然清晰
background: rgba(, , , !important; /*实现FF背景透明,文字不透明*/ filter: Alpha(opacity=); background: #0a0a0a; ...
- BZOJ1069 [SCOI2007]最大土地面积 【凸包 + 旋转卡壳】
题目链接 BZOJ1069 题解 首先四个点一定在凸包上 我们枚举对角线,剩下两个点分别是两侧最远的点 可以三分,复杂度\(O(n^2logn)\) 可以借鉴旋转卡壳的思想,那两个点随着对角线的一定单 ...
- centos上部署nginx服务
Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二的R ...
- java类加载过程以及双亲委派机制
前言:最近两个月公司实行了996上班制,加上了熬了两个通宵上线,状态很不好,头疼.牙疼,一直没有时间和精力写博客,也害怕在这样的状态下写出来的东西出错.为了不让自己荒废学习的劲头和习惯,今天周日,也打 ...
- OpenCV中响应鼠标消息 (转)
#include <cv.h> #include <highgui.h> #include <stdio.h> #pragma comment(lib," ...
- UESTC--1730
原题链接:http://acm.uestc.edu.cn/problem.php?pid=1730 分析:线段树单点更新,区间求和. #include<iostream> #include ...
- windows下安装pthreads扩展注意问题
1.php版本必须是ts版本 2.pthreads扩展下载地址 http://windows.php.net/downloads/pecl/releases/pthreads/ 3.把下载的扩展php ...
- 实例讲解启动mysql server失败的解决方法
MySQL 实例讲解启动mysql server失败的解决方法 来源: 作者: 发表于: 启动mysql server 失败,查看/var/log/mysqld.err 080329 16:01:29 ...
- mac调教指南
最近入手了一个mac, 在此记录下调教的过程. http://note.youdao.com/noteshare?id=5d1eedffab5cb47d4f53970c2ec937aa