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. 【JavaScript算法】---插入排序

    一.什么叫做插入排序法 有一个已经有序的数据序列,要求在这个已经排好的数据序列中插入一个数,但要求插入后此数据序列仍然有序,这个时候就要用到一种新的排序方法——插入排序法 二.核心 插入排序的基本操作 ...

  2. 牛客网多校赛第七场--C Bit Compression【位运算】【暴力】

    链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言524 ...

  3. ubuntu下完全卸载opencv3.1.0

    在ubuntu下删除opencv需要以下步骤: 1.进入opencv的源代码文件夹下的release(这是你在安装opencv时候自己命名的,cmake时候所在的目录) 2.执行以下命令 sudo m ...

  4. Oracle HA 之 RAC one node实战

    --创建rac one node步骤 安装grid软件,配置grid集群:安装oracle软件:dbca创建rac one node. >试验创建的rac one node数据库信息如下: gl ...

  5. python知识大全目录,想学的看过来!

    Python总结篇——知识大全   python装饰器   PyCharm安装与配置,python的Hello World   sort与sorted的区别及实例   我必须得告诉大家的MySQL优化 ...

  6. IOS使用Core-Plot画折线图

    关于Core-Plot的配置.大家能够參考我的上一篇博客:http://1.wildcat.sinaapp.com/?p=99 版权全部.转载请注明原文转自:http://blog.csdn.net/ ...

  7. 邮件服务器Postfix的管理 重启php-fpm

    Postfix邮件系统安装与配置:Postfix,Cyrus-IMAP,Cyrus-sasl,Dovecot和SPFhttp://www.freehao123.com/postfix-cyrus/Ce ...

  8. 004-spring cache-声明性的基于XML的缓存

    一.概述 如果注释不是选项(不能访问源代码或没有外部代码),可以使用XML进行声明式缓存.因此,不是注释用于缓存的方法,而是从外部指定目标方法和缓存指令(类似于声明式事务管理建议). <!-- ...

  9. 分布式版本管理git学习资料整理推荐

    一.什么是git? Git is a free and open source distributed version control system designed to handle everyt ...

  10. tcpdump-抓包工具-Linux

    环境:VMware-Workstation-12-Pro,Windows-10,CentOS-6.9-x86_64,Xshell5 基本介绍 tcpdump是Linux自带的抓包工具,可以详细看到计算 ...