C. Fountains
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 nc 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

Copy
3 7 6
10 8 C
4 3 C
5 6 D
output

Copy
9
input

Copy
2 4 5
2 5 C
2 1 D
output

Copy
0
input

Copy
3 10 10
5 5 C
5 5 C
10 11 D
output

Copy
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.

题意:你有两种货币 C和D 可以买属性为C和D的货物,每种货物有他的beauty值,你现在有若干货币,需要你去买货物C和D得到最大的beauty值

题解:这题理论上来说用n2暴力会T,emmmm但是实际上我们每次取到最大值后break一下的话,可以在1700ms的极限时间跑过去,正解应该是按照beauty值排序后线段树RMQ取区间最值

暴力代码如下:

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define fuck(x) cout<<"["<<x<<"]";
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int maxn = 1e5+;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+;
struct node{
int p,c;
bool operator < (const node &u) const{
return p>u.p;
}
}a[maxn],b[maxn];
int ta,tb;
char ch[];
int u,v;
int main(){
#ifndef ONLINE_JUDGE
FIN
#endif
int n,c,d;
cin>>n>>c>>d;
ta=tb=;
for(int i=;i<n;i++){
cin>>u>>v>>ch;
if(ch[]=='C'){
a[ta].p=u;
a[ta++].c=v;
}else{
b[tb].p=u;
b[tb++].c=v;
}
}
sort(a,a+ta);
sort(b,b+tb);
int ans=;
int pa=,pb=;
for(int i=;i<ta;i++){
if(a[i].c<=c){
pa=a[i].p;
break;
}
}
for(int i=;i<tb;i++){
if(b[i].c<=d){
pb=b[i].p;
break;
}
}
if(pa&&pb) ans=max(ans,pa+pb);
for(int i=;i<ta;i++){
for(int j=i+;j<ta;j++){
if(a[i].c+a[j].c<=c){
ans=max(ans,a[i].p+a[j].p);
break;
}
}
}
for(int i=;i<tb;i++){
for(int j=i+;j<tb;j++){
if(b[i].c+b[j].c<=d){
ans=max(ans,b[i].p+b[j].p);
break;
}
}
}
printf("%d\n",ans);
return ;
}

日天学长写的RMQ代码如下:

#include <bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define x first
#define y second
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=a-1;i>=b;--i)
#define fuck(x) cout<<'['<<#x<<' '<<(x)<<']'
#define eps 1e-12
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef pair<int, int> PII;
const int mod = ; const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fll;
const int MX = 1e5 + ; int v[MX], w[MX];
char t[MX][]; PII p1[MX], p2[MX];
int sz1, sz2; int solve1(int a, int b) {
int mx1 = , mx2 = ;
for(int i = ; i <= sz1; i++) {
if(p1[i].x <= a) mx1 = max(mx1, p1[i].y);
}
for(int i = ; i <= sz2; i++) {
if(p2[i].x <= b) mx2 = max(mx2, p2[i].y);
}
return (mx1 > && mx2 > ) ? (mx1 + mx2) : ;
}
int dp[MX][];
void ST(int n) {
for (int i = ; i <= n; i++) dp[i][] = v[i];
for (int j = ; ( << j) <= n; j++) {
for (int i = ; i + ( << j) - <= n; i++) {
int a = dp[i][j - ] , b = dp[i + ( << (j - ))][j - ];
dp[i][j] = max(a, b);
}
}
}
int RMQ(int l, int r) {
if(l>r) return ;
int k = ;
while (( << (k + )) <= r - l + ) k++;
int a = dp[l][k], b = dp[r - ( << k) + ][k];
return max(a, b);
}
int solve2(PII p[], int sz, int m) {
if(sz <= ) return ;
int ret = ;
sort(p + , p + sz + );
rep(i, , sz + ) w[i] = p[i].x;
rep(i, , sz + ) v[i] = p[i].y;
rep(i, , sz + ) assert(w[i] >= w[i - ]);
ST(sz);
for(int i = , j = sz; i <= sz; i++) {
while(w[i] + w[j] > m && j > i) j--;
if(i < j) ret = max(ret, v[i] + RMQ(i + , j));
}
return ret;
} int main() {
#ifdef local
freopen("in.txt", "r", stdin);
#endif // local int n, a, b; cin >> n >> a >> b;
rep(i, , n + ) scanf("%d%d%s", &v[i], &w[i], t[i]);
rep(i, , n + ) {
if(t[i][] == 'C') p1[++sz1] = PII(w[i], v[i]);
else p2[++sz2] = PII(w[i], v[i]);
}
int ans=solve1(a,b);
ans=max(ans,solve2(p1,sz1,a));
ans=max(ans,solve2(p2,sz2,b));
cout<<ans<<endl;
#ifdef local
cout << "time cost:" << clock() << "ms" << endl;
#endif // local
return ;
}

codeforces 799C Fountains的更多相关文章

  1. 【codeforces 799C】Fountains

    [题目链接]:http://codeforces.com/contest/799/problem/C [题意] 你有两种不同的货币; 余额分别为c和d 然后有n种商品; 每种商品只能由两种货币中的某一 ...

  2. CodeForce-799C Fountains (记忆化DP)

    Fountains CodeForces - 799C 某土豪想要造两座喷泉.现在有 n 个造喷泉的方案,我们已知每个方案的价格以及美观度.有两种合法的货币:金币和钻石.这两种货币之间不能以任何方式转 ...

  3. 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 ...

  4. codeforces 799 C. Fountains(二分+思维)

    题目链接:http://codeforces.com/contest/799/problem/C 题意:要求造2座fountains,可以用钻石,也可以用硬币来造,但是能用的钻石有限,硬币也有限,问能 ...

  5. Codeforces Round #413, rated, Div. 1 + Div. 2 C. Fountains(贪心 or 树状数组)

    http://codeforces.com/contest/799/problem/C 题意: 有n做花园,有人有c个硬币,d个钻石 (2 ≤ n ≤ 100 000, 0 ≤ c, d ≤ 100  ...

  6. 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 ...

  7. 树状数组 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 ...

  8. Codeforces Round #413 (Div1 + Div. 2) C. Fountains(树状数组维护最大值)

    题目链接:https://codeforces.com/problemset/problem/799/C 题意:有 c 块硬币和 d 块钻石,每种喷泉消耗硬币或钻石中的一种,每个喷泉有一个美丽值,问建 ...

  9. 【预处理】【分类讨论】Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains

    分几种情况讨论: (1)仅用C或D买两个 ①买两个代价相同的(实际不同)(排个序) ②买两个代价不同的(因为买两个代价相同的情况已经考虑过了,所以此时对于同一个代价,只需要保存美丽度最高的喷泉即可)( ...

随机推荐

  1. echarts实用小技巧,控制字符串长度,限定整数等

    限定横坐标文本字符长度 xAxis : [ axisLabel:{ formatter: function (value) { var maxlength=6; if (value.length> ...

  2. Java学习笔记七:Java的流程控制语句之switch

    Java条件语句之 switch 当需要对选项进行等值判断时,使用 switch 语句更加简洁明了.例如:根据考试分数,给予前四名不同的奖品.第一名,奖励笔记本一台:第二名,奖励 IPAD 2 一个: ...

  3. 基于jQuery的2048小游戏设计(网页版)

    上周模仿一个2048小游戏,总结一下自己在编写代码的时候遇到的一些坑. 游戏规则:省略,我想大部分人都玩过,不写了 源码地址:https://github.com/xinhua6/2048game.g ...

  4. 【EXCEL】SUMIFS(複数の条件を指定して数値を合計する)

    分享:    

  5. MySQL基础复习

    三范式定义 1NF:每个数据项都是最小单元,不可分割,其实就是确定行列之后只能对应一个数据. 2NF:每一个非主属性完全依赖于候选码(属性组的值能唯一的标识一个元组,但是其子集不可以).  3NF: ...

  6. 【Consul】关于健康检查的一点思考

    健康检查是Consul提供的一项主要功能,其配置格式如下: { "check": { "id": "redis", "name&q ...

  7. 3,SQL语句及数据库优化

       1,统一SQL语句的写法 对于以下两句SQL语句,程序员认为是相同的,数据库查询优化器认为是不同的. 所以封装成复用方法,用标准模板来控制. select*from dual select*Fr ...

  8. 【实用】如何将sublime text 3 打造成实用的python IDE 环境

    前段时间写脚本,一直使用的是pycharm ,无奈机器不配置实在不怎么样,我记得之前用过subline text,这是我用过的最酷炫的文本编辑器,参考了一下网上的文章,自己走了一些弯路,将心得写在这里 ...

  9. virtualBox 安装 CentOs 6.8 以及网络配置

    安装 virtual box 基本设置: 1.创建虚拟电脑 类型:Linux 版本:Red Hat(64-bit) 这个64/32 和电脑具体配置关系. 然后就是路next or 设置常规的东西. 2 ...

  10. 远程连接云主机MySql数据库

    笔者最近在学习MySql数据库,试着远程连接阿里云主机数据库.在连接过程中遇到不少麻烦,这里总结一下过程中遇到的问题. 基本前提 先在本地电脑和远程主机上安装MySql数据库,保证数据库服务启动. 云 ...