http://codeforces.com/contest/799/problem/C

题意:

有n做花园,有人有c个硬币,d个钻石 (2 ≤ n ≤ 100 000, 0 ≤ c, d ≤ 100 000) ,每一个花园用三个维度描述(a,b,c),分别是美丽度,所花钱币个数,钱币种类,当然,钱币之间不能兑换,该人必须要建筑两座花园,如果可以,输出两座花园总的美丽度,否则输出0;

思路:

首先想到分三种情况讨论,两个花园都用硬币,两个花园都用钻石,一个用钻石一个用硬币。

大神的代码真的是很厉害,首先买不起直接省去,接下来先按照价格对花园排序,接下来这个for_max写得很强,记录了第i个之前最大的漂亮度。,接下来进行循环,i从0开始++,然后判断是否满足i<j,然后两座花园所需钱币数量小于目前有的钱币数量,如果满足不断使i++,这样做是满足贪心要求,你目前求得必然是在当前j的条件下最大的,最终求出答案。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
using namespace std; const int maxn=+;
typedef pair<int,int> pll; int n,c,d;
vector<pll> CC,DD;
int for_max[maxn]; int solve(vector<pll> z,int money)
{
int len=z.size();
if(len<) return ;
int ans=;
sort(z.begin(),z.end()); //先按照价格排序
for_max[]=;
for(int i=;i<len;i++) for_max[i+]=max(for_max[i],z[i].second); //预处理,计算出前i个当中最大的漂亮度
int i=;
for(int j=len-;j>=;j--)
{
while(i<j && z[i].first+z[j].first<=money) i++; //因为for_max[i]记录了前i-1个的最大漂亮度,所以只要没超过总价格,就可以一直加上去
i=min(i,j);
if(i>)
ans=max(ans,for_max[i]+z[j].second);
}
return ans;
} int main()
{
//freopen("D:\\input.txt","r",stdin);
while(~scanf("%d%d%d",&n,&c,&d))
{
CC.clear(); DD.clear();
int cc_max=,dd_max=;
int ans=;
int b,p; char s[];
for(int i=;i<n;i++)
{
scanf("%d%d%s",&b,&p,&s);
if(s[]=='C')
{
if(p>c) continue; //买不起的直接省去
CC.push_back(make_pair(p,b));
cc_max=max(cc_max,b);
}
else
{
if(p>d) continue;
DD.push_back(make_pair(p,b));
dd_max=max(dd_max,b);
}
}
if(cc_max!= && dd_max!=) ans=cc_max+dd_max; //第一种情况
ans=max(ans,solve(CC,c)); //两个都是硬币
ans=max(ans,solve(DD,d)); //两个都是钻石
printf("%d\n",ans);
}
return ;
}

另外,树状数组也是可以做的,可以参考http://blog.csdn.net/ssimple_y/article/details/71702915

Codeforces Round #413, rated, Div. 1 + Div. 2 C. Fountains(贪心 or 树状数组)的更多相关文章

  1. 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 ...

  2. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 树状数组

    E. DNA Evolution 题目连接: http://codeforces.com/contest/828/problem/E Description Everyone knows that D ...

  3. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)

    题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...

  4. Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)

    http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...

  5. VK Cup 2016 - Round 1 (Div. 2 Edition) B. Bear and Displayed Friends 树状数组

    B. Bear and Displayed Friends 题目连接: http://www.codeforces.com/contest/658/problem/B Description Lima ...

  6. CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组

    题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有 ...

  7. CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组(转)

    转载自:http://www.cnblogs.com/icode-girl/p/5744409.html 题目链接:CF #365 (Div. 2) D - Mishka and Interestin ...

  8. Testing Round #12 A,B,C 讨论,贪心,树状数组优化dp

    题目链接:http://codeforces.com/contest/597 A. Divisibility time limit per test 1 second memory limit per ...

  9. Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)(A.暴力,B.优先队列,C.dp乱搞)

    A. Carrot Cakes time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

随机推荐

  1. 【BZOJ1001】[BeiJing2006]狼抓兔子 对偶图最短路

    [BZOJ1001][BeiJing2006]狼抓兔子 Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子 ...

  2. windows 下,go语言 交叉编译

    http://bbs.csdn.net/topics/390601048 参考上面的操作

  3. 【Android】eclipse打不开的解决办法和“Jar mismatch! Fix your dependencies”的解决

    JDK1.7能用,cmd下输入java,javac,java -version,javaw配置和环境都没问题的话,有可能是工作空间的问题,就是一般在D盘下的workspace..那个文件夹,删除了,再 ...

  4. java的synchronized有没有同步的类锁?

    转自:http://langgufu.iteye.com/blog/2152608 http://www.cnblogs.com/beiyetengqing/p/6213437.html 没有... ...

  5. 【node】------mongoose的基本使用------【巷子】

    1.安装mongoose npm install mongoose 2.启动数据库 mongod --dbpath d:\data\db 3.引入mongoose模块并连接数据库 const mong ...

  6. RedisDesktopManager 打开报0xc000007b程序错误

    RedisDesktopManager 是一个管理redis的工具,很好用,我的电脑可以安装0.8.3版的,最新版到0.9.4了,其中经典版本是0.8.8,可惜0.8.3版之后,我的电脑安装软件后,打 ...

  7. 每次收到的 HTTP 请求,就可以打开一个 SqlSession,返回一个响应,就关闭它

    mybatis – MyBatis 3 | 入门 http://www.mybatis.org/mybatis-3/zh/getting-started.html 作用域(Scope)和生命周期 理解 ...

  8. Spring 依赖注入 基于构造函数、设值函数、内部Beans、集合注入

    Spring 基于构造函数的依赖注入_w3cschool https://www.w3cschool.cn/wkspring/t7n41mm7.html Spring 基于构造函数的依赖注入 当容器调 ...

  9. ping 10.13.5.233

    3. 环境 URL选择器 tableView ping 10.13.5.233

  10. 第1章 1.2计算机网络概述--Intenet和广域网

    1.ISP Internet服务提供商,也称ISP运营商.如:在中国,电信.网通.长城宽带. ISP运营商内部的网络连接十分发达,但是不同ISP运营商之间的连接的线路非常有限,这就导致了跨运营商访问网 ...