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买两个 ①买两个代价相同的(实际不同)(排个序) ②买两个代价不同的(因为买两个代价相同的情况已经考虑过了,所以此时对于同一个代价,只需要保存美丽度最高的喷泉即可)( ...
随机推荐
- jenkins邮件发送jmeter接口测试报告
在Jenkins中配置实现邮件通知,Jenkins提供了两种方式的配置. 一种是Jenkins内置默认的邮件通知,但是它本身有很多局限性,比如它的邮件通知无法提供详细的邮件内容.无法定义发送邮件的格式 ...
- Vijos 纸牌
题目网址 https://vijos.org/d/Randle/p/5a0011e1d3d8a10a532d6d71 题目描述 在桌面上放着n张纸牌,每张纸牌有两面,每面都写着一个非负整数.你的邪王真 ...
- 【Leetcode】804. Unique Morse Code Words
Unique Morse Code Words Description International Morse Code defines a standard encoding where each ...
- (2)分布式下的爬虫Scrapy应该如何做-关于对Scrapy的反思和核心对象的介绍
本篇主要介绍对于一个爬虫框架的思考和,核心部件的介绍,以及常规的思考方法: 一,猜想 我们说的爬虫,一般至少要包含几个基本要素: 1.请求发送对象(sender,对于request的封装,防止被封) ...
- 利用LD_PRELOAD进行hook
原文地址:http://hbprotoss.github.io/posts/li-yong-ld_preloadjin-xing-hook.html 好久没玩hook这种猥琐的东西里,今天在Linux ...
- 在 Ubuntu 16.04 LTS 上安装 Python 3.6.0
原文连接:https://segmentfault.com/a/1190000007912666 最近 Python 3 发布了新版本 Python 3.6.0,好像又加入了不少黑魔法!- 由于暂时不 ...
- SpriteKit游戏开发适配iPad/iPhone6/7/8/Plus及iPhoneX的尺寸及安全区域
未适配前:Ball球超过屏幕的上下方 适配后:Ball球就在屏幕的可视范围内运动了 一.那么如何适配不同的iPhone.iPhoneX及iPad的屏幕尺寸呢? 我们开发一个App的时候, 通常希望 ...
- 03-Mysql数据库----安装与管理
本节掌握内容: mysql的安装.启动 mysql破解密码 统一字符编码 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的 ...
- visionpro halcon 哪个好
visionpro halcon 哪个好 很多朋友会问到visionpro和halcon这两款机器视觉软件,到底学哪个好呢,今天众寻网就给大家讲一讲: 首先比较下两者的优缺点: halcon: 提供的 ...
- android仿QQ的SlideMenu
这其实很简单就可以实现,只需要自定义一个View继承自HorizontalScrollView 1,新建一个项目,再新建一个MySlideMenu继承HorizontalScrollView publ ...