poj3040 Allowance
思路:
贪心。
看了题解说是
先把面值从大到小排序
然后从头往尾扫,只要不超额,能取多少去多少
然后如果还有剩余,就从尾往头扫,尽量取,让他恰好超额
不过并不懂证明。
实现:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; int N, C;
struct node
{
int d, c;
};
node a[]; bool cmp(const node & a, const node & b)
{
return a.d > b.d;
} int main()
{
cin >> N >> C;
for (int i = ; i < N; i++)
{
scanf("%d %d", &a[i].d, &a[i].c);
}
sort(a, a + N, cmp);
int cnt = ;
int j = ;
for (; j < N; j++)
{
if (a[j].d < C)
break;
}
for (int i = ; i < j; i++)
{
cnt += (a[i].d / C) * a[i].c;
}
while (true)
{
int now = ;
for (int i = j; i < N; i++)
{
while (a[i].c && now + a[i].d <= C)
{
now += a[i].d;
a[i].c--;
}
}
for (int i = N - ; i >= j; i--)
{
while (a[i].c && now < C)
{
now += a[i].d;
a[i].c--;
}
}
if (now < C)
break;
cnt++;
}
cout << cnt << endl;
return ;
}
poj3040 Allowance的更多相关文章
- poj-3040 Allowance (贪心)
http://poj.org/problem?id=3040 FJ 有n种不同面值的硬币,每种硬币都有相应的个数,大面值的硬币值总能被小面值的硬币值整除,每周需要支付 Bessie c元,问最多能 ...
- 《挑战程序设计竞赛》2.2 贪心法-其它 POJ3617 3069 3253 2393 1017 3040 1862 3262
POJ3617 Best Cow Line 题意 给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下列任意操作: 从S的头部(或尾部)删除一个字符,加到T的尾部 ...
- 【POJ - 3040】Allowance(贪心)
Allowance 原文是English,这里就放Chinese了 Descriptions: 作为创纪录的牛奶生产的奖励,农场主约翰决定开始给Bessie奶牛一个小的每周津贴.FJ有一套硬币N种(1 ...
- poj 3040 Allowance
Allowance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1842 Accepted: 763 Descript ...
- bzoj:1685 [Usaco2005 Oct]Allowance 津贴
Description As a reward for record milk production, Farmer John has decided to start paying Bessie t ...
- P2376 [USACO09OCT]津贴Allowance
P2376 [USACO09OCT]津贴Allowance一开始想的是多重背包,但是实践不了.实际是贪心,让多c尽可能少,所以先放大的,最后让小的来弥补. #include<iostream&g ...
- 洛谷 P2376 [USACO09OCT]津贴Allowance 解题报告
P2376 [USACO09OCT]津贴Allowance 题目描述 作为创造产奶纪录的回报,\(Farmer\) \(John\)决定开始每个星期给\(Bessie\)一点零花钱. \(FJ\)有一 ...
- 【贪心算法】POJ-3040 局部最优到全局最优
一.题目 Description As a reward for record milk production, Farmer John has decided to start paying Bes ...
- 【BZOJ】1685: [Usaco2005 Oct]Allowance 津贴(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1685 由于每个小的都能整除大的,那么我们在取完大的以后(不超过c)后,再取一个最小的数来补充,可以证 ...
随机推荐
- android findVIewById()在线生成工具
今天突然发现一个好工具,能够依据你输入的XML布局文件,自己主动生成findViewById的代码.还支持android annotation的注解方式.真是太棒了.由于我正是使用androidann ...
- Visual Studio Code Unit Testing
1.NUnit project.json { "version": "1.0.0-*", "testRunner": "nunit ...
- dm385的分辨率切换
建议用两个RSZ的输出来完成切换分辨率功能,帧率可以通过软件丢帧来实现. 两个SWMS增加了两个1080p60的读和写,对系统影响是比较大的. http://www.deyisupport.com/q ...
- 关于集成支付宝SDK的开发
下载 首先,你要想找到这个SDK,都得费点功夫.如今的SDK改名叫移动支付集成开发包了,下载页面在 这里 的 "请点此下载集成开发包" Baidu和Googlep排在前面的支付宝开 ...
- SSH无密码验证可能出现的问题
雪影工作室版权所有,转载请注明[http://blog.csdn.net/lina791211] 一.安装和启动SSH协议 假设没有安装ssh和rsync,可以通过下面命令进行安装. sudo apt ...
- 两大数相乘 -- javascript 实现
(function (){ var addLarge = function(n1,n2){ var carry = 0; var ret = ""; n1=n1.toString( ...
- Lightoj 1014 - Ifter Party
I have an Ifter party at the 5th day of Ramadan for the contestants. For this reason I have invited ...
- SPOJ:Fibonacci Polynomial(矩阵递推&前缀和)
Problem description. The Fibonacci numbers defined as f(n) = f(n-1) + f(n-2) where f0 = 0 and f1 = 1 ...
- [Usaco2017 Feb]Why Did the Cow Cross the RoadII
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4993 [算法] 动态规划 转移类似于求LCS [代码] #include<bi ...
- Splay基本操作
我们以一道题来引入吧! 传送门 题目说的很清楚,我们的数据结构要支持:插入x数,删除x数,查询数的排名和排名为x的数,求一个数前驱后继. 似乎用啥现有的数据结构都很难做到在O(nlogn)的复杂度中把 ...