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也一定是最大值,因此我们不排序直接这样进行查询更新是非常合理的。
代码实现如下:
1 #include <set> 2 #include <map> 3 #include <queue> 4 #include <stack> 5 #include <cmath> 6 #include <ctime> 7 #include <bitset> 8 #include <cstdio> 9 #include <string> 10 #include <vector> 11 #include <cstdlib> 12 #include <cstring> 13 #include <iostream> 14 #include <algorithm> 15 using namespace std; 16 17 typedef long long ll; 18 typedef pair<ll, ll> pll; 19 typedef pair<ll, int> pli; 20 typedef pair<int, ll> pil;; 21 typedef pair<int, int> pii; 22 typedef unsigned long long ull; 23 24 #define lson i<<1 25 #define rson i<<1|1 26 #define bug printf("*********\n"); 27 #define FIN freopen("D://code//in.txt", "r", stdin); 28 #define debug(x) cout<<"["<<x<<"]" <<endl; 29 #define IO ios::sync_with_stdio(false),cin.tie(0); 30 31 const double eps = 1e-8; 32 const int mod = 1000000007; 33 const int maxn = 1e5 + 7; 34 const double pi = acos(-1); 35 const int inf = 0x3f3f3f3f; 36 const ll INF = 0x3f3f3f3f3f3f3f; 37 38 int n, c, d, bea, cost, mx1, mx2, t1, t2; 39 char op[6]; 40 41 struct node { 42 int bea, cost; 43 node(int bea = 0, int cost = 0) : bea(bea), cost(cost) {} 44 }num1[maxn], num2[maxn]; 45 46 struct tree { 47 int l, r, mx; 48 }segtree[maxn*4]; 49 50 void push_up(int i) { 51 segtree[i].mx = max(segtree[i*2].mx, segtree[i*2+1].mx); 52 } 53 54 void build(int i, int l, int r) { 55 segtree[i].l = l, segtree[i].r = r; 56 if(l == r) { 57 segtree[i].mx = 0; 58 return; 59 } 60 int mid = (l + r) >> 1; 61 build(i * 2, l, mid); 62 build(i * 2 + 1, mid + 1, r); 63 push_up(i); 64 } 65 66 void update(int i, int pos, int val) { 67 if(segtree[i].l == pos && segtree[i].r == pos) { 68 segtree[i].mx = max(segtree[i].mx, val); 69 return; 70 } 71 int mid = (segtree[i].l + segtree[i].r) >> 1; 72 if(pos <= mid) update(i*2, pos, val); 73 else update(i*2+1, pos, val); 74 push_up(i); 75 } 76 77 int query(int i, int l, int r) { 78 if(segtree[i].l == l && segtree[i].r == r) { 79 return segtree[i].mx; 80 } 81 int mid = (segtree[i].l + segtree[i].r) >> 1; 82 if(r <= mid) return query(i*2, l, r); 83 else if(l > mid) return query(i*2+1, l, r); 84 else return max(query(i*2, l, mid), query(i*2+1, mid + 1, r)); 85 } 86 87 int main() { 88 scanf("%d%d%d", &n, &c, &d); 89 mx1 = -1, mx2 = -1; 90 for(int i = 1; i <= n; i++) { 91 scanf("%d%d%s", &bea, &cost, op); 92 if(op[0] == 'C') { 93 if(cost <= c && bea > mx1) { 94 mx1 = bea; 95 } 96 num1[++t1] = node(bea, cost); 97 } else { 98 if(cost <= d && bea > mx2) { 99 mx2 = bea; 100 } 101 num2[++t2] = node(bea, cost); 102 } 103 } 104 int ans = 0; 105 build(1, 0, maxn); //因为C-num[i].cost可能等于0,所以线段树的区间需要从0开始 106 for(int i = 1; i <= t1; i++) { 107 if(c >= num1[i].cost) { 108 int t = query(1, 0, c - num1[i].cost); 109 if(t != 0) { 110 ans = max(ans, t + num1[i].bea); 111 } 112 } 113 update(1, num1[i].cost, num1[i].bea); 114 } 115 build(1, 0, maxn); 116 for(int i = 1; i <= t2; i++) { 117 if(d >= num2[i].cost) { 118 int t = query(1, 0, d - num2[i].cost); 119 if(t != 0) { 120 ans = max(ans, t + num2[i].bea); 121 } 122 } 123 update(1, num2[i].cost, num2[i].bea); 124 } 125 if(mx1 != -1 && mx2 != -1) { //一个用钻石支付,一个用现金支付 126 ans = max(ans, mx1 + mx2); 127 } 128 printf("%d\n", ans); 129 return 0; 130 }
转载于:https://www.cnblogs.com/Dillonh/p/9496940.html
C.Fountains(Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)+线段树+RMQ)...相关推荐
- Codeforces Round #352 div1 C D (智商+线段树)
Ultimate Weirdness of an Array 题意:给一个数列ai, f(i,j) 定义为除去i到j之间的数后最大gcd(ai, aj)的值, 求所有f(i,j)之和 #include ...
- Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)切题报告(A-B题)
这是我第二次参加CF比赛,也是第一次写博客,写得不好,望各位大佬海涵. A. Single Wildcard Pattern Matching 原题链接 这道题,这是难到让人无法直视,太水了. 题目大 ...
- Educational Codeforces Round 114 (Rated for Div. 2) (A ~ F)全题解
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Educational Codeforces Round 114 (Rated for Div. 2) ...
- Educational Codeforces Round 106 (Rated for Div. 2)(A ~ E)题解(每日训练 Day.16 )
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 Educational Codeforces Round 106 (Rated for Div. ...
- Educational Codeforces Round 37 (Rated for Div. 2) 1
Educational Codeforces Round 37 (Rated for Div. 2) A.Water The Garden 题意:Max想给花园浇水.花园可被视为长度为n的花园床,花园 ...
- Educational Codeforces Round 90 (Rated for Div. 2)(A, B, C, D, E)
Educational Codeforces Round 90 (Rated for Div. 2) Donut Shops 思路 分三种情况: a==c/ba == c / ba==c/b这个时候两 ...
- Educational Codeforces Round 89 (Rated for Div. 2)(A, B, C, D)
Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords 思路 题意非常简单,就是得到最多的物品嘛,我们假定a, ...
- Educational Codeforces Round 114 (Rated for Div. 2) D. The Strongest Build 暴力 + bfs
传送门 文章目录 题意: 思路: 题意: 你有nnn个装备槽,每个槽里面有cic_ici个力量加成,对于每个槽只能选一个力量加成,现在给你mmm个力量组合[b1,b2,...,bn][b_1,b_2 ...
- Educational Codeforces Round 72 (Rated for Div. 2) D. Coloring Edges dfs树/拓扑找环
传送门 文章目录 题意: 思路: 题意: 给你一张图,你需要给这个图的边染色,保证如果有环那么这个环内边的颜色不全相同,输出染色方案和用的颜色个数. n,m≤5e3n,m\le5e3n,m≤5e3 思 ...
- Educational Codeforces Round 111 (Rated for Div. 2) D. Excellent Arrays 组合数学
传送门 文章目录 题意: 思路: 题意: 给你一个数组aia_iai,定义一个数组是好的当且仅当对于所有iii都有ai!=ia_i!=iai!=i.定义f(a)f(a)f(a)表示数组aaa中i& ...
最新文章
- POJ 3080 多个串最长公共子序列
- python介绍和用途-python数据类型介绍与使用
- mysql 一对多映射_mybatis关系映射之一对多和多对一
- u盘linux软件下载,u盘linux制作工具(Universal USB Installer)
- Android之SparseArray<E>详解
- redis 哨兵_Redis哨兵机制的原理介绍
- Oracle Number用法
- centos操作系统版本获取
- RISC-V学习资料:《手把手教你设计CPU——RISC-V处理器》
- xmind试用模式会过期吗_汽车发动机机油「保质期」概念解析:机油真的会过期吗?...
- 对抗神经网络(Adversarial Nets)的介绍[1]
- mac快捷键修改跟windows一样_如何把Windows按键改成苹果按键
- python 战棋游戏代码实现(1):生物行走和攻击选择
- CVPR--2019 AI CITY CHALLENGE (track1成绩A榜第一,综合第二)
- 作计算机报告用英语怎么写,计算机专业英语报告.doc
- python thinker 简易计算器
- strip,lstrip,rstrip,sprit(字符串处理)
- NEON 常用函数讲解
- 辐射3特殊武器拿法(修正版)
- 导航条形式 转自百度UEO
热门文章
- [渝粤教育] 广东-国家-开放大学 21秋期末考试大学英语210262k2
- 【渝粤教育】国家开放大学2018年春季 0554-22T立体构成(一) 参考试题
- LeetCode刷题系列(二)二分查找、二叉排序树 的应用
- Python基础:字典(dict)与集合(set)
- 2018.1.30-31 开始racket,避免mutation,lazy evaluation
- ShopEx 中规格属性添加时,自己主动计算其相应的销售价格,同一时候注意模板中的变量间的计算...
- 【转】Prewitt 算子
- java中排序函数sort()使用,Arrays.sort()和Collections.sort()
- vue-router的简单理解
- 2018国庆雅礼D3T1