codeforces 799C Fountains
2 seconds
256 megabytes
standard input
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.
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.
Print the maximum total beauty of exactly two fountains Arkady can build. If he can't build two fountains, print 0.
3 7 6
10 8 C
4 3 C
5 6 D
9
2 4 5
2 5 C
2 1 D
0
3 10 10
5 5 C
5 5 C
10 11 D
10
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的更多相关文章
- 【codeforces 799C】Fountains
[题目链接]:http://codeforces.com/contest/799/problem/C [题意] 你有两种不同的货币; 余额分别为c和d 然后有n种商品; 每种商品只能由两种货币中的某一 ...
- CodeForce-799C Fountains (记忆化DP)
Fountains CodeForces - 799C 某土豪想要造两座喷泉.现在有 n 个造喷泉的方案,我们已知每个方案的价格以及美观度.有两种合法的货币:金币和钻石.这两种货币之间不能以任何方式转 ...
- 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 ...
- codeforces 799 C. Fountains(二分+思维)
题目链接:http://codeforces.com/contest/799/problem/C 题意:要求造2座fountains,可以用钻石,也可以用硬币来造,但是能用的钻石有限,硬币也有限,问能 ...
- 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 ...
- 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 ...
- 树状数组 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 ...
- Codeforces Round #413 (Div1 + Div. 2) C. Fountains(树状数组维护最大值)
题目链接:https://codeforces.com/problemset/problem/799/C 题意:有 c 块硬币和 d 块钻石,每种喷泉消耗硬币或钻石中的一种,每个喷泉有一个美丽值,问建 ...
- 【预处理】【分类讨论】Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains
分几种情况讨论: (1)仅用C或D买两个 ①买两个代价相同的(实际不同)(排个序) ②买两个代价不同的(因为买两个代价相同的情况已经考虑过了,所以此时对于同一个代价,只需要保存美丽度最高的喷泉即可)( ...
随机推荐
- 关于JUnit4无法支持多线程测试的解决方法
转自:https://segmentfault.com/a/1190000003762719 其实junit是将test作为参数传递给了TestRunner的main函数.并通过main函数进行执行. ...
- labview初始学习过程中遇到串口读取框红蓝色交替闪烁的处理
labview工程的程序框图VISA串口读取框红蓝交替闪烁,前面板接收数据错乱,或者是接受不了,这是你不小心设置了断点.
- narcissus
public class narcissus { public static void main(String args[]) { long u=0,t=0,h=0,y=0,k=0; for(long ...
- Matplotlib 基本图表的绘制
图表类别:线形图.柱状图.密度图,以横纵坐标两个维度为主 同时可延展出多种其他图表样式 plt.plot(kind='line', ax=None, figsize=None, use_index=T ...
- 【转】Android开发之ListView+EditText-要命的焦点和软键盘问题解决办法
Android开发之ListView+EditText-要命的焦点和软键盘问题解决办法 [原文链接] 这篇文章完美的解决了我几个月没结论的bug... 感谢热爱分享的技术达人~ 我是怎么走进这个大坑的 ...
- angularjs post data
//post json 时收不到数据,目前只找到方法post form形式的key-value值 //关键是设置 headers: { 'Content-Type': 'application/x- ...
- Ehcache缓存实例
一:目录 EhCache 简介 Hello World 示例 Spring 整合 Dummy CacheManager 的配置和作用 二: 简介 1. 基本介绍 EhCache 是一个纯Java的进程 ...
- 初步学习pg_control文件之十五
接前文 初步学习pg_control文件之十四 再看如下这个: int MaxConnections; 应该说,它是一个参考值,在global.c中有如下定义 /* * Primary determ ...
- linux redhat 打开防火墙中的某个端口
服务器成功监听了一个端口(如 5500),但是外面连接不进来,telnet其端口不通,解决办法如下(在root用户下): $ /sbin/iptables -I INPUT -p tcp --dpor ...
- Django admin操作
无名小妖 昵称:无名小妖园龄:1年6个月粉丝:22关注:1 +加关注 搜索 常用链接 我的随笔 我的评论 我的参与 最新评论 我的标签 我的标签 Python(1) python3 ...