codeforces Round #413 Div. 1 + Div. 2 C Fountains
来源:互联网 发布:weka apriori算法 编辑:程序博客网 时间:2024/05/18 02:41
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 610 8 C4 3 C5 6 D
9
2 4 52 5 C2 1 D
0
3 10 105 5 C5 5 C10 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.
二叉搜索树~
看起来好简单……可是注意的地方好多……说起来这还是我第一次写二叉搜索树啊……调到吐血……
(考试的时候写了个奇怪的贪心算法100多行,结果调不出来了……所以代码一定要精简啊)
总共只有三种情况:C类和D类各拿一个,两个都是C类,两个都是D类。
在输入的时候就处理一下,把没法买的去掉,刚好只能买一个的记录一下价值也去掉。
第一种情况可以在输入的时候直接记录一下,如果两类都有,就更新答案。
第二类和第三类一样,只要清空一下树就可以了。
我们把所有能取到的物品排序,花费是第一关键字,价值是第二关键字。那么每次在二叉树中搜索能与它匹配的最大价值,更新答案,再把它放到二叉树中。
注意:
1.花费大的不一定价值大;
2.不能合并同花费物品,因为同花费物品也可能组成一对;
3.要去掉不能与任何物品组队的物品;
4.二叉树中搜索的是[1,x]这个区间;
5.要先搜索再加入,这样可以保证不会与自己组队。
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;#define N 100005int n,c,d,x,y,a[N<<2],tot1,tot2,max1,max2,maxx;bool b1,b2;char s[2];struct node{int cos,val;}numa[N],numb[N];bool operator < (node u,node v){return u.cos==v.cos ? u.val<v.val:u.cos<v.cos;}int read(){int x=0,f=1;char ch=getchar();while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}while(ch>='0' && ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}void add(int l,int r,int k,int u,int v){if(l==r){a[k]=max(a[k],v);return;}int mid=l+r>>1;if(u<=mid) add(l,mid,k<<1,u,v);else add(mid+1,r,k<<1|1,u,v);a[k]=max(a[k<<1],a[k<<1|1]);}int findd(int l,int r,int k,int u){if(r<=u) return a[k];int mid=l+r>>1;if(u<=mid) return findd(l,mid,k<<1,u);return max(findd(l,mid,k<<1,u),findd(mid+1,r,k<<1|1,u));}int main(){n=read();c=read();d=read();for(int i=1;i<=n;i++){x=read();y=read();scanf("%s",s);if(s[0]=='C' && y<c) numa[++tot1]=(node){y,x},max1=max(max1,x),b1=1;else if(s[0]=='D' && y<d) numb[++tot2]=(node){y,x},max2=max(max2,x),b2=1;else if(s[0]=='C' && y==c) max1=max(max1,x),b1=1;else if(s[0]=='D' && y==d) max2=max(max2,x),b2=1;}maxx=b1*b2 ? max1+max2:0;if(tot1>1){sort(numa+1,numa+tot1+1);for(int i=1;i<=tot1;i++){x=c-numa[i].cos;y=numa[i].val;int z=findd(1,100000,1,x);maxx=z ? max(maxx,y+z):maxx;add(1,100000,1,numa[i].cos,y);}}if(tot2>1){if(tot1>1) memset(a,0,sizeof(a));sort(numb+1,numb+tot2+1);for(int i=1;i<=tot2;i++){x=d-numb[i].cos;y=numb[i].val;int z=findd(1,100000,1,x);maxx=z ? max(maxx,y+z):maxx;add(1,100000,1,numb[i].cos,y);}}printf("%d\n",maxx);return 0;}
- Codeforces Round #413 Div. 1 + Div. 2 C. Fountains
- codeforces Round #413 Div. 1 + Div. 2 C Fountains
- Codeforces Round #413 Div. 2 C. Fountains
- CodeforcesPlayrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)C题C. Fountains
- Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains(树状数组)
- Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C Fountains 乱搞
- Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C Fountains
- Codeforces Round #413:C. Fountains
- Codeforces Round #413 C. Fountains
- codeforces round #413 C. Fountains
- Codeforces Round #413(Div. 1 + Div. 2)
- CF Round #413( Div.1/2) Fountains(线状数组查找)
- Codeforces Round #413 C. Fountains 二分
- Codeforces Round #413 C-Fountains 树状数组
- Codeforces Round #230 (Div. 2) C / (Div. 1) A
- Codeforces Round #230 (Div. 2) C (Div. 1)
- Codeforces Round #400 (Div. 1 + Div. 2, combined) C
- 【codeforces】Codeforces Round #370 (Div. 2) C
- 本周的博客----第12周
- MyBatis批量插入返回主键
- django连接mysql
- mini2440通过dnw下载linux虚拟机的程序
- 开发简单的CMS
- codeforces Round #413 Div. 1 + Div. 2 C Fountains
- JAVA递归
- 快速自定义漂亮的Dialog
- 华东师范大学-E. 黑心啤酒厂
- eclipse数据表映射为实体类
- JS原型与原型链详解
- 之前runtime的坑
- 我会成为一个优秀的程序员吗
- 网络编程