【POJ - 3040】Allowance(贪心)
Allowance
原文是English,这里就放Chinese了
Descriptions:
Input
* 第 2..N+1行: 硬币价值 V (1 <= V <= 100,000,000) 和数量 B (1 <= B <= 1,000,000)
Output
Sample Input
3 6
10 1
1 100
5 120
Sample Output
111
Hint
FJ给Bessie 10美分一周;给Bessie 5美分和1美分一百周;给Bessie 两枚5美分十周
题目链接:
https://vjudge.net/problem/POJ-3040
题目大意:FJ要发硬币工资给他的奶牛,工资不低于c,他有n个硬币,币值和数目。问你最多发多少个星期
1)从大面值到小面值一次拿钱,能拿多少拿多少。
但是注意不能拿到的钱的总和大于C
2)如果第一步拿到的钱不够C,那么就从小面值到大面值拿钱,能拿多少拿多少。
直到拿到的钱总和大于等于C
AC代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define ME0(x) memset(x,0,sizeof(x))
using namespace std;
pair<int,int> a[];//硬币面值,数量
int use[];//这个面值的硬币已经使用了多少
int n,c;//硬币种类,每周最少给多少钱
int main()
{
cin>>n>>c;
for(int i=; i<=n; i++)
cin>>a[i].first>>a[i].second;
sort(a+,a++n);
int ans=;//答案
while(true)
{
ME0(use);//每次都归零
int rest=c;//还有这么多钱没给奶牛
for(int i=n; i>=; i--)//从大到小
{
int tmp=min(rest/a[i].first,a[i].second);//这种类型的硬币能拿几个
rest-=tmp*a[i].first;//还有多少钱没给奶牛
use[i]=tmp;//这种硬币用了几个
}
if(rest)//还有钱没有发完
{
for(int i=; i<=n; i++)//从小到大
{
if(a[i].second&&a[i].first>=rest)//这种硬币没有花完并且这种硬币的价值大于没有发完的钱
{
use[i]++;//这种硬币用了一个
rest=;//钱全部发给奶牛了
break;
}
}
}
if(rest)//钱还没发完,即钱不够了,退出循环
break;
int minx=INF;
for(int i=; i<=n; i++)//从小到大
{
if(use[i])//这种硬币一周要用几个,可以算出剩余的硬币可以用多少周
minx=min(minx,a[i].second/use[i]);
}
ans+=minx;
for(int i=; i<=n; i++)//把硬币的数量更新
{
if(use[i])
a[i].second-=use[i]*minx;
}
}
cout<<ans<<endl;
}
【POJ - 3040】Allowance(贪心)的更多相关文章
- POJ 3040 Allowance 贪心
这题目的贪心思路还是有一点细节问题的. 还没有证明,据说是因为题目给的条件是每个价格是比它小的价格的倍数才能这么贪心的. 思路如下: 假设要给奶牛的钱为C 1)从大面值到小面值一次拿钱,能拿多少拿多少 ...
- POJ 3040 Allowance【贪心】
POJ 3040 题意: 给奶牛发工资,每周至少 C 元.约翰手头上有面值V_i的硬币B_i个,这些硬币的最小公约数为硬币的最小面值.求最多能发几周? 分析: 贪心策略是使多发的面额最小(最优解).分 ...
- poj 3040 Allowance
Allowance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1842 Accepted: 763 Descript ...
- 【贪心】Allowance POJ 3040
题目链接:http://poj.org/problem?id=3040 题目大意:你有n种不同面值的硬币,面值为vi的有bi个."硬币的面额均匀地分配下一个更大的面额",即下一个更 ...
- POJ 3040 贪心
贪心好题 ---. 思路: 从大到小凑C 如果不够 再从小到大补满(超过)C //By SiriusRen #include <cstdio> #include <cstring&g ...
- Greedy:Allowance(POJ 3040)
零用钱大作战 题目大意:农夫和牛又搞新花样了,现在农夫想给Bessie每个星期都给一点零用钱,农夫有一堆面值的钱币,并且这个钱币都能被上一个钱币整除(1,5,10,50),并且钱币有一定数量,要你求最 ...
- poj-3040 Allowance (贪心)
http://poj.org/problem?id=3040 FJ 有n种不同面值的硬币,每种硬币都有相应的个数,大面值的硬币值总能被小面值的硬币值整除,每周需要支付 Bessie c元,问最多能 ...
- POJ 1456(贪心)
#include <string.h> #include <iostream> #include <queue> #include <stdio.h> ...
- poj -3614 Sunscreen(贪心 + 优先队列)
http://poj.org/problem?id=3614 有c头奶牛在沙滩上晒太阳,每头奶牛能忍受的阳光强度有一个最大值(max_spf) 和最小值(min_spf),奶牛有L种防晒霜,每种可以固 ...
随机推荐
- ReentrantLock和Synchronized
1 synchronized 1.1 一旦没有获取到就只能一直等待 A和B都获取同一个对象锁,如果A获取了,B没有获取到,那么在A释放该锁之前,B只能无穷等待下去. 1.2 synchronized是 ...
- rule-based optimizer cost-based optimizer
SQL processing uses the following main components to execute a SQL query: The Parser checks both syn ...
- (非原)SQL注入专题--整理帖 && like 语句拼sql 如何防止注入攻击。
原地址:blog.csdn.net/lvjin110/article/details/28697695 like 语句拼sql 如何防止注入攻击?http://bbs.csdn.net/topics/ ...
- px dp 互转
/** * * px dip 转换 */ public class DensityUtil { /** * 根据手机分辨率 dip 转 px */ static public int dip2px(C ...
- android系统启动框架、Activity界面显示过程详解
一.Android系统框架 android的系统架构和其操作系统一样,采用了分层的架构.从架构图看,android分为四个层,从高层到低层分别是应用程序层.应用程序框架层.系统运行库层和linux核心 ...
- CSS中float与A标签的疑问
<stype> a{ text-decoration:none; float:left;} </stype> <div class="box1"> ...
- 常用的Css命名方式
常用的Css命名方式: CSS命名规范: 1.文件命名规范 全局样式:global.css: 框架布局:layout.css: 字体样式:font.css: 链接样式:link.css: 打印样式:p ...
- LightOJ1341 Aladdin and the Flying Carpet —— 唯一分解定理
题目链接:https://vjudge.net/problem/LightOJ-1341 1341 - Aladdin and the Flying Carpet PDF (English) S ...
- 实用jQuery代码片段
maco精选的一些jQuery代码,也许你从中可以举一反三[代码] [JavaScript]代码001<p>002 <h3><span >★ 使用jQuery ...
- codeforces A. Fox and Box Accumulation 解题报告
题目链接:http://codeforces.com/problemset/problem/388/A 题目意思:有 n 个 boxes,每个box 有相同的 size 和 weight,但是stre ...