【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的那个点,我们准备 ...
随机推荐
- 算法笔记_031:计算中值和选择问题(Java)
目录 1 问题描述 2 解决方案 2.1 计算中值问题 2.2 选择问题 1 问题描述 中值问题是求一个n个数列表中某一数组下标k,它要求该下标元素比列表中的一半元素大,又比另一半元素小,这个中 ...
- Tags Used In OpenERP 7.0
In OpenERP 7.0. the form view of each object has been redesigned so that the object the user is work ...
- sql server 数据库备份历史记录
sql server 数据库备份历史记录 SELECT ),SERVERPROPERTY('Servername'))AS Server, bs.database_name, bs.backup_st ...
- java.lang.Void and void
java.lang.Void is analogous to java.lang.Integer. Integer is a way of boxing values of the primitive ...
- 编译VLC for IOS
之前接触VLC是因为Winrt的项目,后来似乎ARM版本的始终搞不定(没有针对于ARM-COFF的GCC编译器),vlc for winrt的项目好久没有更新了,自己也没有深入研究.有一天跟同事聊,他 ...
- hibernate中错误笔记
1.在写Student.hbm.xml 中, hibernate-mapping 中 指定类和数据库对应的表字段时,不小心将property写为properties,报错: ERROR: HHH000 ...
- 【转载】php如何给APP端写接口
如何写好接口 目的:清楚明白所写接口的用途 安全性:做好接口的安全性,防止接口数据泄露,做好必要的参数加密措施 按需分配: 接受值和返回值要实用,不接受和返回不需要的数据,返回值返回什么应与使用者时时 ...
- nginx实战二
nginx架构分析 1.nginx模块化 Nginx涉及到的模块分为核心模块.标准HTTP模块.可选HTTP模块.邮件服务模块以及第三方模块等五大类. https://coding.net/u/ami ...
- ItelliJ基于Gradle创建及发布Web项目(三)
关键字:web 多模块 用惯了eclipse傻瓜式的配置,开始web部署真的不习惯. 一.现象: 项目发布了,死活找不到依赖模块中的类. 二.排查 确定F4->Artifacts->Out ...
- ToString(“N2”)和ToString(“0.00”)之间的区别
看来N会包含数千个分隔符,而0.00则不会. N2将以500.00的方式工作,但是当您有5000.00时,N2将显示为 5,000.00 代替 5000.00 If you do this inste ...