思路:

贪心。

看了题解说是

先把面值从大到小排序
然后从头往尾扫,只要不超额,能取多少去多少
然后如果还有剩余,就从尾往头扫,尽量取,让他恰好超额

不过并不懂证明。

实现:

 #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的更多相关文章

  1. poj-3040 Allowance (贪心)

    http://poj.org/problem?id=3040 FJ 有n种不同面值的硬币,每种硬币都有相应的个数,大面值的硬币值总能被小面值的硬币值整除,每周需要支付 Bessie   c元,问最多能 ...

  2. 《挑战程序设计竞赛》2.2 贪心法-其它 POJ3617 3069 3253 2393 1017 3040 1862 3262

    POJ3617 Best Cow Line 题意 给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下列任意操作: 从S的头部(或尾部)删除一个字符,加到T的尾部 ...

  3. 【POJ - 3040】Allowance(贪心)

    Allowance 原文是English,这里就放Chinese了 Descriptions: 作为创纪录的牛奶生产的奖励,农场主约翰决定开始给Bessie奶牛一个小的每周津贴.FJ有一套硬币N种(1 ...

  4. poj 3040 Allowance

    Allowance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1842   Accepted: 763 Descript ...

  5. bzoj:1685 [Usaco2005 Oct]Allowance 津贴

    Description As a reward for record milk production, Farmer John has decided to start paying Bessie t ...

  6. P2376 [USACO09OCT]津贴Allowance

    P2376 [USACO09OCT]津贴Allowance一开始想的是多重背包,但是实践不了.实际是贪心,让多c尽可能少,所以先放大的,最后让小的来弥补. #include<iostream&g ...

  7. 洛谷 P2376 [USACO09OCT]津贴Allowance 解题报告

    P2376 [USACO09OCT]津贴Allowance 题目描述 作为创造产奶纪录的回报,\(Farmer\) \(John\)决定开始每个星期给\(Bessie\)一点零花钱. \(FJ\)有一 ...

  8. 【贪心算法】POJ-3040 局部最优到全局最优

    一.题目 Description As a reward for record milk production, Farmer John has decided to start paying Bes ...

  9. 【BZOJ】1685: [Usaco2005 Oct]Allowance 津贴(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1685 由于每个小的都能整除大的,那么我们在取完大的以后(不超过c)后,再取一个最小的数来补充,可以证 ...

随机推荐

  1. EXTJS 4 树形表格组件使用演示样例

    EXTJS 4 树形表格组件使用演示样例 一.总体效果图 version=1&modificationDate=1412058826000&api=v2" alt=" ...

  2. C语言 字符串操作 笔记

    /* C语言字符串的操作笔记 使用代码和注释结合方式记录 */ # include <stdio.h> # include <string.h> int main(void) ...

  3. 修正iOS从照相机和相册中获取的图片 方向

    修正iOS从照相机和相册中获取的图片 方向   修正iOS从照相机和相册中获取的图片 方向 使用系统相机拍照得到的图片的默认方向有时不是ImageOrientationDown,而是ImageOrie ...

  4. 使用PowerShell 创建SharePoint 站点

    使用PowerShell 创建SharePoint 站点         在SharePoint开发中,你应该学会使用PowerShell管理SharePoint网站.SharePoint Manag ...

  5. Hibernate 之 Mapping

    转自:  http://blog.csdn.net/jnqqls/article/details/8372732 从前面的介绍的Hibernate文章中我们已经对Hibernate有了一个初步的认识, ...

  6. java操作json

    import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class ReadJson { public static v ...

  7. How to Execute Page_Load() in Page's Base Class?

    https://stackoverflow.com/questions/2737092/how-to-execute-page-load-in-pages-base-class We faced th ...

  8. YTU 2438: 三人三鬼

    2438: 三人三鬼 时间限制: 1 Sec  内存限制: 128 MB 提交: 35  解决: 9 题目描述 目标是将东岸的3人3鬼通过一只小船转移到西岸,希望以尽可能少的摆渡次数. 船的容量有限, ...

  9. YTU 2960: 改错题--小鼠标,你要干什吗?

    2960: 改错题--小鼠标,你要干什吗? 时间限制: 1 Sec  内存限制: 128 MB 提交: 118  解决: 62 题目描述 鼠标双击不同的图标产生不同的效果,比如双击文档(documen ...

  10. 二:网络--GET请求和POST请求

    一.GET请求和POST请求简单说明 GET - 从指定的服务器中获取数据 POST - 提交数据给指定的服务器处理 GET方法: 使用GET方法时,查询字符串(键值对)被附加在URL地址后面一起发送 ...