【BZOJ】1685: [Usaco2005 Oct]Allowance 津贴(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1685
由于每个小的都能整除大的,那么我们在取完大的以后(不超过c)后,再取一个最小的数来补充,可以证明这是最优的。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=25;
int n, l, r;
int ans, c;
struct dat { int x, y; }a[N];
bool operator < (const dat &a, const dat &b) { return a.x<b.x; } bool ask() {
int sum=0;
for3(i, r, l) {
int t=(c-sum)/a[i].x;
t=min(t, a[i].y);
sum+=t*a[i].x; a[i].y-=t;
if(sum==c) return 1;
}
if(sum==c) return 1;
while(a[l].y==0 && l<=r) ++l;
while(a[r].y==0 && l<=r) --r;
if(l>r) return 0;
while(l<=r && sum<c) {
int t=(c-sum+a[l].x-1)/a[l].x;
if(t<=a[l].y) { a[l].y-=t; return 1; }
else { sum+=a[l].y*a[l].x; a[l].y=0; ++l; }
}
if(sum<c) return 0;
return 1;
}
int main() {
read(n); read(c);
for1(i, 1, n) read(a[i].x), read(a[i].y);
sort(a+1, a+1+n);
l=1; r=n;
while(a[r].x>=c && l<=r) ans+=a[r].y, a[r].y=0, --r;
while(ask()) ++ans;
printf("%d", ans);
return 0;
}
Description
As a reward for record milk production, Farmer John has decided to start paying Bessie the cow a small weekly allowance. FJ has a set of coins in N (1 <= N <= 20) different denominations, where each denomination of coin evenly divides the next-larger denomination (e.g., 1 cent coins, 5 cent coins, 10 cent coins, and 50 cent coins). Using the given set of coins, he would like to pay Bessie at least some given amount of money C (1 <= C <= 100,000,000) every week. Please help him compute the maximum number of weeks he can pay Bessie.
Input
Output
Sample Input
10 1
1 1 00
5 1 20
Sample Output
样例说明
约翰想要每周付给贝茜6美分.他有1个10美分的硬币、100个1美分的硬币、120个5美分的硬币.约翰可以第一周付给贝茜一个10美分的硬币,接着的
10周每周付给贝茜2个5芙分硬币,接下来的100周每周付给贝茜一个1美分的硬币和1个5美分的硬币.共计111周.
HINT
Source
【BZOJ】1685: [Usaco2005 Oct]Allowance 津贴(贪心)的更多相关文章
- BZOJ 1685 [Usaco2005 Oct]Allowance 津贴:贪心【给硬币问题】
题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1333 题意: 有n种不同币值的硬币,并保证大币值一定是小币值的倍数. 每种硬币的币值为 ...
- bzoj:1685 [Usaco2005 Oct]Allowance 津贴
Description As a reward for record milk production, Farmer John has decided to start paying Bessie t ...
- BZOJ1685: [Usaco2005 Oct]Allowance 津贴
[传送门:BZOJ1685] 简要题意: 贝西工作勤勤恳恳,她每月向约翰索要C 元钱作为工资.约翰手上有不少钱,他一共有N 种面 额的钞票.第i 种钞票的面额记作Vi,约翰有Ki 张.钞票的面额设定是 ...
- BZOJ 1684: [Usaco2005 Oct]Close Encounter
题目 1684: [Usaco2005 Oct]Close Encounter Time Limit: 5 Sec Memory Limit: 64 MB Description Lacking e ...
- bzoj 1684: [Usaco2005 Oct]Close Encounter【数学(?)】
枚举分母,然后离他最近的分子只有两个,分别判断一下能不能用来更新答案即可 #include<iostream> #include<cstdio> #include<cma ...
- bzoj1745[Usaco2005 oct]Flying Right 飞行航班*
bzoj1745[Usaco2005 oct]Flying Right 飞行航班 题意: n个农场,有k群牛要从一个农场到另一个农场(每群由一只或几只奶牛组成)飞机白天从农场1到农场n,晚上从农场n到 ...
- 1684: [Usaco2005 Oct]Close Encounter
1684: [Usaco2005 Oct]Close Encounter Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 387 Solved: 181[ ...
- [BZOJ 2006] [NOI 2010]超级钢琴(贪心+ST表+堆)
[BZOJ 2006] [NOI 2010]超级钢琴(贪心+ST表+堆) 题面 给出一个长度为n的序列,选k段长度在L到R之间的区间,一个区间的值等于区间内所有元素之的和,使得k个区间的值之和最大.区 ...
- bzoj1745: [Usaco2005 oct]Flying Right 飞行航班(贪心+map)
之前做过一道基本一样的题目,抽象出来就是有个容量为c的载体,一些线段上某个点到另一个点要运输w个东西,求从头到尾最多能运多少东西. 这种模型可以用贪心做,用map,map[r]表示r的那个点,我们准备 ...
随机推荐
- 开发移动 APP 时,你应注意这 5 个细节
智能手机的普及带动了大批移动应用的诞生,这些应用能够帮助人们解决日常生活所面临的种种问题.Smart Insights 发表的一份报告指出,移动应用占人们使用智能手机总时间的89%,因此,为了确保你所 ...
- linux&shell
Linux经常使用命令 登录时显示信息放在/etc/motd和/etc/profile.d/xxx.sh motd放置字符串 profile.d下放置脚本文件 echo echo -e 处理特殊字符. ...
- 通过源码浅析Java中的资源加载
前提 最近在做一个基础组件项目刚好需要用到JDK中的资源加载,这里说到的资源包括类文件和其他静态资源,刚好需要重新补充一下类加载器和资源加载的相关知识,整理成一篇文章. 理解类的工作原理 这一节主要分 ...
- Python enumerate索引迭代
索引迭代Python中,迭代永远是取出元素本身,而非元素的索引.对于有序集合,元素确实是有索引的.有的时候,我们确实想在 for 循环中拿到索引,怎么办?方法是使用 enumerate() 函数:&g ...
- 转:sock_ev——linux平台socket事件框架(基于数据报的测试程序) .
上一篇已经做过注释,这一篇直接上代码 /******************************************************************************** ...
- activeMQ Jms Demo
概述 ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经 ...
- 移动UI设计中需要避免的四种常见用户体验误区
2012年移动应用的下载量超过300 亿,可是智能手机用户平均每周会使用的应用数却大概只有15个.更糟的是,Localytics 的研究表明,大概有22%的应用是见光死,用过一次之后就被束之高阁.既然 ...
- 使用HTML5的Notification API制作web通知的教程(转)
var notification=new Notification(‘Notification Title',{ body:'Your Message' }); 上面的代码构造了一个简陋的通知栏.构造 ...
- unity5, import fbx注意事项
一,模型尺寸. unity中是以米为单位,要想让3dmax中建的模型导入到unity中与unity自带的3d object尺寸一致,最直接的办法是将3dmax中的系统单位也设成米. 菜单->自定 ...
- 解决Eclipse无法打开“Failed to load the JNI shared library”
这是因为JDK配置错误所导致的现象. 一般说来,新购笔记本会预装64位的windows系统,而在网上下载软件时,32位会优先出现在页面中(现在来说是这个情况,但我认为未来64位会越来越普及). 如果你 ...