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.
要求建两个喷泉,现在有n个喷泉可以选,每一个喷泉的价格和漂亮度都已经给出,这里有两种货币,硬币和钻石(王者荣耀的感觉,买英雄啊,买两个加起来最强的,你有一定的金币和钻石,用钻石买的英雄肯定有比较强的,也可能没有)。求出他能建的喷泉的方法中最大的漂亮度。
这个我只能想到超时的做法,n^2的,正确的打开方式是树状数组.要建两个喷泉,一共就三种情况,选一个用硬币买的喷泉再选一个用钻石买的喷泉,或者选两个用硬币买的喷泉,或者选两个用钻石买的喷泉。
所以枚举每一个喷泉,然后用树状数组查询出,这样大大降低了复杂度。这样的话二分也可以过应该。看来是时候学一波线段树,树状数组了
来个树状数组入门
1
2
3
|
int lowbit( int x){ return x&(x^(x–1)); } |
1
2
3
|
int lowbit( int x){ return x&-x; } |
#include<bits/stdc++.h> using namespace std; const int maxn = 100000; int C[maxn+10],D[maxn+10]; void add(int *tree,int k,int num) { while(k<=maxn) { tree[k] = max(tree[k],num); k+=k&-k; } } int read(int *tree,int k) { int res=0; while(k) { res = max(res,tree[k]); k-=k&-k; } return res; } int main() { int n,c,d,i,j; scanf("%d%d%d",&n,&c,&d); int ans = 0; for(i=1; i<=n; i++) { int b,p; char t[5]; scanf("%d%d%s",&b,&p,t); int maxn; if(t[0] == 'C') { maxn = read(D,d); if(p > c) continue; maxn = max(maxn,read(C,c-p)); add(C,p,b); } else { maxn = read(C,c); if(p > d) continue; maxn = max(maxn,read(D,d-p)); add(D,p,b); } if(maxn) ans = max(ans,maxn + b); } cout << ans << endl; return 0; }