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)后,再取一个最小的数来补充,可以证 ... 
随机推荐
- [转] 买彩票的利器--gun
			源链接 还在自己买彩票吗,有个现成的:GNU shuf命令. shuf -i - -n | 这样就会产生两组彩票(1~36个数字任选) 当然还可以派其他用途,比如: shuf -e clubs hea ... 
- 3 微信开发本地代理环境的搭建--实现将内网ip映射到外网
			微信公众号的开发,要搭建网站,并且随时都有可能修改网站内容进行调试,这就需要临时外网能返回本地开发环境搭建的项目进行测试,即内网映射到公网,但是好多开发者没有自己的域名和服务器,这里我们先来搭建一个本 ... 
- 第一个WordCount类运行
			import java.io.IOException; import java.util.*; import org.apache.hadoop.fs.Path; import org.apache. ... 
- Posting array of JSON objects to MVC3 action method via jQuery ajax
			Does the model binder not suport arrays of JSON objects? The code below works when sending a single ... 
- DataTables warning requested unknown parameter
			This is possibly the most cryptic warning message that DataTables will show. It is a short error mes ... 
- Lightoj 1017 - Brush (III)
			1017 - Brush (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Sam ... 
- Mac Launchpad图标调整
			Launchpad图标大小怎么调整?,很多人觉得默认Launchpad的应用程序图标很大,空间比较拥挤,看起来一点也不精致,那么我们怎样才能调整Launchpad的图标大小呢?其实可以通过调整Laun ... 
- 自己动手写最简单的Android驱动---LED驱动的编写【转】
			本文转载自:http://blog.csdn.net/k_linux_man/article/details/7023824 转载注明出处,作者:K_Linux_Man, 薛凯 山东中医药大学,给文章 ... 
- 关于Vim的一个配置文件
			昨天晚上+今天早上怒赶了一份关于Vim的自动化配置的Shell脚本,之前在github上见过一个这么一个类似的脚本项目,然后又见到同校的有一位师兄也写过这么一个类似的脚本文件,然后我也抽分跟着写一份属 ... 
- HDU - 1150 Machine Schedule(最小点覆盖数)
			1.有两台机器A和B以及N个需要运行的任务.A机器有n种不同的模式,B机器有m种不同的模式,而每个任务都恰好在一台机器上运行.如果它在机器A上运行,则机器A需要设置为模式xi,如果它在机器B上运行,则 ... 
