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. 静态栈抽象数据类型stack实现

    #include<stdio.h> #include<stdbool.h> #include<stdlib.h> #define MAX_STACK_SIZE 10 ...

  2. go学习笔记-语言指针

    语言指针 定义及使用 变量是一种使用方便的占位符,用于引用计算机内存地址.取地址符是 &,放到一个变量前使用就会返回相应变量的内存地址. 一个指针变量指向了一个值的内存地址.类似于变量和常量, ...

  3. 最小生成树算法 1.Prim算法

    最小生成树(MST):一个有N个点的图,边一定是大于等于N-1条边的.在这些边中选择N-1条出来,连接所有N个点.这N-1条边的边权之和是所有方案中最小的. Prim算法的时间复杂度时O(n^2)的, ...

  4. 永无BUG 注释

    /** *                   _ooOoo_ *                  o8888888o *                  88" . "88 ...

  5. XML与Object的范型转换

    前段时间做object转换xml想了很多,所有打算整理下 做成以下的通用方法. public static bool ObjectToXml<T>(string filePath, T t ...

  6. Java并发(多线程)

    一.多线程的基本概念 1.什么是进程.多进程有什么作用? 大家都使用计算机,当我们打开某一个软件的时候,其实就是启动了一个进程,可以打开任务管理器看看,我们打开的每一个软件,都是一个进程,在同一个操作 ...

  7. 第二篇 Python初识别及变量名定义规范

    第一个Python程序 可以打开notepad或者其他文本编辑器,输入:print("Hello Python!"),将文件保存到任意盘符下,后缀名是  .py 两种python程 ...

  8. Java中的while(true)

    while(true)是一个无限循环 在内部用break或return退出循环,否则一直循环

  9. Hibernate 查询,返回结果设置到DTO

    1:原生sql的查询,返回结果设置到DTO: Query query = sessionFactoryRtData.getCurrentSession().createSQLQuery(hql.toS ...

  10. rcnn caffe matlab 配置完成 14.04 cuda 7.0

    http://blog.csdn.net/real_myth/article/details/42672381 各种痛苦.实验室网速还是龟速. 莫名其妙的错误. gcc还降级到4.7,opencv 3 ...