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 ...
随机推荐
- Android ContentProvider基本用法
转自:https://www.jianshu.com/p/601086916c8f 一.基本概念 ContentProvider是Android系统中提供的专门用户不同应用间进行数据共享的组件,提供了 ...
- jdbc 小结
1,PreparedStatement/Statement区别: 1,防止sql注入式攻击(sql注入:就是通过非正常手段(比如在url中添加参数)),将sql文执行(比如or 1=1) 2,Prep ...
- 搭建属于自己的NuGet服务器
文章导读 创建NuGetServer Web站点 发布站点到IIS 添加本地站点到包包数据源 在上一篇NuGet学习笔记(2) 使用图形化界面打包自己的类库 中讲解了如何打包自己的类库,接下来进行最重 ...
- 【.Net】C#获取Windows系统特殊文件夹的路径
系统特殊文件夹是包含公共信息的文件夹,如“Program Files”.“Programs”.“System”或“Startup”.特殊文件夹在默认情况下由系统设置,或者由用户在安装 Windows ...
- BZOJ 1799 同类分布(数位DP)
给出a,b,求出[a,b]中各位数字之和能整除原数的数的个数.1<=a<=b<=1e18. 注意到各位数字之和最大是153.考虑枚举这个东西.那么需要统计的是[0,a-1]和[0,b ...
- 洛谷 P1972 [SDOI2009]HH的项链
不是裸题,鉴定完毕. 我是题面 对于这道题,我是离线做的... 树状数组吧,好些点 我们可以很轻易地得到一个很显然的结论,就是关于同一个数,我们只需要记录它不超过当前区间的最后一次出现的位置即可.举例 ...
- JAVA中的堆、栈等内存分析
在 JAVA 中,有六个不同的地方可以存储数据 1. 寄存器( register ) 这是最快的存储区,因为它位于不同于其他存储区的地方——处理器内部.但是寄存器的数量极其有限,所以寄存器由编译器根据 ...
- 【MVVM 原生】原生MVVM的使用
一.前言 前些天需要完成一个任务,该任务属于公司的一些核心代码,为了避免不必要的麻烦,任务要求不能使用第三方的MVVM框架,必须用原生的. 平时习惯了Dev与MVVMLight,遇上原生的 ...
- linux(二) 基本使用命令
一.常用命令归纳分类 课外网站 http://man.linuxde.net/ http://www.jb51.net/linux/ http ...
- mysql数据库----python操作mysql ------pymysql和SQLAchemy
本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy 一.pymysql pymsql是Python中操作MySQL的模块,其使用方法和MySQ ...