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. C语言Windows程序开发—CreateWindow函数介绍【第03天】

    (一)CreateWindow函数的参数介绍: HWND CreateWindow( LPCTSTR lpClassName, //Windows窗口中预定义的控件结构体,包括:BUTTON(按钮), ...

  2. 让CPU使用率正弦变化

    网络上流传一个面试题,说如何编程让CPU的使用率按照正弦方式变化 代码如下(运行环境Linux): #include <stdio.h> #include <stdlib.h> ...

  3. go学习笔记-变量作用域

    变量作用域 作用域为已声明标识符所表示的常量.类型.变量.函数或包在源代码中的作用范围. 变量可以在三个地方声明: 函数内定义的变量称为局部变量 函数外定义的变量称为全局变量 函数定义中的变量称为形式 ...

  4. YSZOJ:#247. [福利]可持久化线段树 (最适合可持久化线段树入门)

    题目链接:https://syzoj.com/problem/247 解题心得: 可持久化线段树其实就是一个线段树功能的加强版,加强在哪里呢?那就是如果一颗普通的线段树多次修改之后还能知道最开始的线段 ...

  5. ssrf小记

    SSRF(Server-Side Request Forgery, 服务端请求伪造),攻击者伪造服务端发起的请求并执行,从而获得一些数据或进行攻击 一.危害 1.对内网的端口和服务进行扫描,对主机本地 ...

  6. 开启一个项目如何上传到git

    1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库 git init 2.把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点 ...

  7. RelativeSource设定绑定方向

    <Window x:Class="Yingbao.Chapter2.RelativeEx.AppWin" xmlns="http://schemas.microso ...

  8. MyEclipse - 问题集 - 创建Maven项目,JDK版本默认是1.5

    修改Maven的配置文件settings.xml,增加profile节点,如下所示: <profile> <id>jdk-1.8</id> <activati ...

  9. 怎么防止别人动态在你程序生成代码(怎么防止别人反编译你的app)

    1.本地数据加密 iOS应用防反编译加密技术之一:对NSUserDefaults,sqlite存储文件数据加密,保护帐号和关键信息 2.URL编码加密 iOS应用防反编译加密技术之二:对程序中出现的U ...

  10. vue2.0 $emit $on组件通信

    在vue1.0中父子组件通信使用$dispatch 和 $broadcast,但是在vue2.0中$dispatch 和 $broadcast 已经被弃用. 因为基于组件树结构的事件流方式实在是让人难 ...