Fountains(非线段树版(主要是不太会用))
Arkady plays Gardenscapes a lot. Arkady wants to build two new fountains. There are n available fountains, for each fountain its beauty and cost are known. There are two types of money in the game: coins and diamonds, so each fountain cost can be either in coins or diamonds. No money changes between the types are allowed.
Help Arkady to find two fountains with maximum total beauty so that he can buy both at the same time.
Input
The first line contains three integers n, c and d (2 ≤ n ≤ 100 000, 0 ≤ c, d ≤ 100 000) — the number of fountains, the number of coins and diamonds Arkady has.
The next n lines describe fountains. Each of these lines contain two integers bi and pi (1 ≤ bi, pi ≤ 100 000) — the beauty and the cost of the i-th fountain, and then a letter "C" or "D", describing in which type of money is the cost of fountain i: in coins or in diamonds, respectively.
Output
Print the maximum total beauty of exactly two fountains Arkady can build. If he can't build two fountains, print 0.
Examples
Input
3 7 6
10 8 C
4 3 C
5 6 D
Output
9
Input
2 4 5
2 5 C
2 1 D
Output
0
Input
3 10 10
5 5 C
5 5 C
10 11 D
Output
10
Note
In the first example Arkady should build the second fountain with beauty 4, which costs 3 coins. The first fountain he can't build because he don't have enough coins. Also Arkady should build the third fountain with beauty 5 which costs 6 diamonds. Thus the total beauty of built fountains is 9.
In the second example there are two fountains, but Arkady can't build both of them, because he needs 5 coins for the first fountain, and Arkady has only 4 coins.
思路:见代码
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#define MAX 100005
using namespace std;
typedef long long ll;
int n,c,d,b,cost,cnt,maxs;
char type;
struct node{
int b,cost;
char type;
};
node f[MAX];
bool cmp(node x,node y) {
if(x.b!=y.b)
return x.b>y.b;
else
{
return x.cost<y.cost;
}
}//排序按照美丽值排序,如果相等就按照花费小排
int main() {
while(scanf("%d%d%d",&n,&c,&d)!=EOF) {
maxs=0;
for(int i=0; i<n; i++) {
scanf("%d%d %c",&b,&cost,&type);
f[i].b=b;
f[i].cost=cost;
f[i].type=type;
}//输入
sort(f,f+n,cmp);
int max1=0,max2=0;
for(int i=0; i<n; i++) {
if(f[i].type=='C'&&f[i].cost<=c) {
max1=f[i].b;
break;
}
}
for(int i=0; i<n; i++) {
if(f[i].type=='D'&&f[i].cost<=d) {
max2=f[i].b;
break;
}
}//第一种情况,在用硬币的和用钻石的分别一个
int maxxn=0;
//这种情况必须保证都能得到,如果有一个不够就直接是原来的0就行了,表示这种情况不成立
if(max1!=0&&max2!=0) {
maxxn=max1+max2;
}
//第二种情况,都从用硬币的取两个或者都从用钻石的取两个,这样枚举的时候是有技巧的,不然n^2必然超时
//也要庆幸数据没有都卡到最后
for(int i=0; i<n; i++) {
int t1=c;
int t2=d;
if(f[i].type=='C'&&t1<=f[i].cost)
continue;
else if(f[i].type=='D'&&t2<=f[i].cost)
continue;
if(f[i].type=='C'&&t1>f[i].cost) {
t1-=f[i].cost;
cnt=0;
cnt+=f[i].b;
} else if(f[i].type=='D'&&t2>f[i].cost) {
t2-=f[i].cost;
cnt=0;
cnt+=f[i].b;
}
for(int j=i+1; j<n; j++) {
if(f[j].type=='C'&&f[j].cost<=t1) {
cnt+=f[j].b;
maxs=max(maxs,cnt);
break;
} else if(f[j].type=='D'&&f[j].cost<=t2) {
cnt+=f[j].b;
maxs=max(maxs,cnt);
break;
}
}
}
printf("%d\n",max(maxs,maxxn));
}
return 0;
}
Fountains(非线段树版(主要是不太会用))的更多相关文章
- Matrix(线段树版)
poj2155:http://poj.org/problem?id=2155 题意;同上一遍随笔. 题解:这里用二维线段树打了一发.第一次学习别人的代码.才学的.这种树套树的程序,确实很费脑子,一不小 ...
- BZOJ2028:[SHOI2009]会场预约(线段树版)
浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://www.lydsy.com/JudgeOnline/prob ...
- Color the ball HDU - 1556 (非线段树做法)
题意:在1到n的气球中,在不同的区域中涂颜色,问每个气球涂几次. #include<cstdio>int num[100010];int main(){ int n, x, y;; whi ...
- hdu 1556 Color the ball(非线段树做法)
#include<stdio.h> #include<string.h> ]; int main() { int n,i; int a,b; while(scanf(" ...
- ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】
FZU 2105 Digits Count Time Limit:10000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- zkw线段树详解
转载自:http://blog.csdn.net/qq_18455665/article/details/50989113 前言 首先说说出处: 清华大学 张昆玮(zkw) - ppt <统计的 ...
- Luogu P3740 [HAOI2014]贴海报_线段树
线段树版的海报 实际上这个与普通的线段树相差不大,只是貌似数据太水,暴力都可以过啊 本来以为要离散的,结果没打就A了 #include<iostream> #include<cstd ...
- 洛谷.3733.[HAOI2017]八纵八横(线性基 线段树分治 bitset)
LOJ 洛谷 最基本的思路同BZOJ2115 Xor,将图中所有环的异或和插入线性基,求一下线性基中数的异或最大值. 用bitset优化一下,暴力的复杂度是\(O(\frac{qmL^2}{w})\) ...
- Gym 101911E "Painting the Fence"(线段树区间更新+双端队列)
传送门 题意: 庭院中有 n 个围栏,每个围栏上都被涂上了不同的颜色(数字表示): 有 m 条指令,每条指令给出一个整数 x ,你要做的就是将区间[ x第一次出现的位置 , x最后出现的位置 ]中的围 ...
随机推荐
- Linux Valgrind命令
一.简介 C/C++程序,最常见的错误之一就是内存泄露.Valgrind 是一款 Linux下的内存调试工具,它可以对编译后的二进制程序进行内存使用监测找出内存泄漏问题. Valgrind通常包括如下 ...
- HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)
题意:给定 n 个数,要你将其分成m + 1组,要求每组数必须是连续的而且要求得到的价值最小.一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0. 析:DP状态方程 ...
- oracle列出两个日期间所有日期
select date '2010-1-1'+level-1 dates from dual connect by level <date '2010-1-10' -date '2010-1-1 ...
- Java静态变量的用法:伪单例
这几天遇到一个问题,一个Service里有一个map,但是这个Service有别的继承,于是每一个Service都会创建一个map,但是这个map应该是公用的,于是就有问题了...(按结构说Servi ...
- VSCode调试C#控制台与单元测试
公司前端最近项目里面在用VSCode编写前端代码,觉得这个编辑器很轻便,既然是微软出的,肯定支持C#,就去网上查了查资料,发现还真是支持C#,并且蛮多地方用到dotnet命令,哈哈. 1.powers ...
- docker系列 参考文章
Docker 系列一(概念原理和安装) Docker 系列二(操作镜像) Docker 系列三(容器管理) 持续更新... ubuntu 安装docker 参考文章 :(https://blog.cs ...
- [Algorithm]图
一.图的算法 邻接矩阵表示的数据结构 1 #define INFINITY INT_MAX // 无穷大 2 #define MAX_VERTEX_NUM 20 // 限制顶点最大数值为20个 3 # ...
- react.js学习之路三
学习react.js,知识点整理: 1.props和state: props是相对于父级来说,固定的不会改变的内容.一般会先定义一个变量,则在父级中进行引用, var user = "liu ...
- 【bzoj2437】[Noi2011]兔兔与蛋蛋 二分图最大匹配+博弈论
Description Input 输入的第一行包含两个正整数 n.m. 接下来 n行描述初始棋盘.其中第i 行包含 m个字符,每个字符都是大写英文字母"X".大写英文字母&quo ...
- loj#2978. 「THUSCH 2017」杜老师(乱搞)
题面 传送门 题解 感谢yx巨巨 如果一个数是完全平方数,那么它的所有质因子个数都是偶数 我们把每一个数分别维护它的每一个质因子的奇偶性,那么就是要我们选出若干个数使得所有质因子的个数为偶数.如果用线 ...