CodeForces 16B Burglar and Matches(贪心)
0.5 second
64 megabytes
standard input
standard output
A burglar got into a matches warehouse and wants to steal as many matches as possible. In the warehouse there are mcontainers, in the i-th
container there are ai matchboxes,
and each matchbox contains bi matches.
All the matchboxes are of the same size. The burglar's rucksack can hold n matchboxes exactly. Your task is to find out the maximum amount of matches that
a burglar can carry away. He has no time to rearrange matches in the matchboxes, that's why he just chooses not more than n matchboxes so that the total
amount of matches in them is maximal.
The first line of the input contains integer n (1 ≤ n ≤ 2·108)
and integer m (1 ≤ m ≤ 20). The i + 1-th
line contains a pair of numbers ai and bi (1 ≤ ai ≤ 108, 1 ≤ bi ≤ 10).
All the input numbers are integer.
Output the only number — answer to the problem.
7 3
5 10
2 5
3 6
62
3 3
1 3
2 2
3 1
7
非常水的贪心 直接每次选装的多的盒子 n减去盒子数即可了
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int M = 25;
int a[M], b[M], c[M], n, m, ans; bool cmp (int i, int j)
{
return b[i] > b[j];
} int main()
{
scanf ("%d%d", &n, &m);
for (int i = 1; i <= m; ++i)
{
c[i] = i;
scanf ("%d%d", &a[i], &b[i]);
}
sort (c + 1, c + m + 1, cmp);
ans = 0;
for (int i = 1; i <= m; ++i)
{
if (n >= a[c[i]])
{
n -= a[c[i]];
ans += a[c[i]] * b[c[i]];
}
else
{
ans += n * b[c[i]];
break;
}
}
printf ("%d\n", ans);
return 0;
}
CodeForces 16B Burglar and Matches(贪心)的更多相关文章
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- [Codeforces 1214A]Optimal Currency Exchange(贪心)
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...
- codeforces 349B Color the Fence 贪心,思维
1.codeforces 349B Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...
- Codeforces Gym 100269E Energy Tycoon 贪心
题目链接:http://codeforces.com/gym/100269/attachments 题意: 有长度为n个格子,你有两种操作,1是放一个长度为1的东西上去,2是放一个长度为2的东西上去 ...
- CodeForces 797C Minimal string:贪心+模拟
题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...
- codeforces 803D Magazine Ad(二分+贪心)
Magazine Ad 题目链接:http://codeforces.com/contest/803/problem/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个数字k,和一行字符 例: g ...
- Codeforces 980E The Number Games 贪心 倍增表
原文链接https://www.cnblogs.com/zhouzhendong/p/9074226.html 题目传送门 - Codeforces 980E 题意 $\rm Codeforces$ ...
- Codeforces 798D Mike and distribution - 贪心
Mike has always been thinking about the harshness of social inequality. He's so obsessed with it tha ...
- Codeforces 402D Upgrading Array:贪心 + 数学
题目链接:http://codeforces.com/problemset/problem/402/D 题意: 给你一个长度为n的数列a[i],又给出了m个“坏质数”b[i]. 定义函数f(s),其中 ...
随机推荐
- 生产环境4.3.5jboss内存调优
1.查看jboss的监控工具 http://XXX/jmx-console/htmladaptor 2.查看jvm的监控工具 jdk\bin jvisualvm.exe jmc.exe 3.查看jbo ...
- Java中PrintStream(打印输出流)
Java中PrintStream(打印输出流) PrintStream 是打印输出流,它继承于FilterOutputStream. PrintStream 是用来装饰其它输出流.它能为其他输出流 ...
- nw.js开发第一个程序(html开发桌面程序exe)
一.环境配置 windows系统 cnpm install node 下载nw.js https://github.com/nwjs/nw.js 找到download下载合适的版本 二.开发 项目目录 ...
- Detectron:Pytorch-Caffe2-Detectron的一些跟进
pytorch官网:http://pytorch.org/上只有PyTroch的ubuntu和Mac版本,赤裸裸地歧视了一把Windows低端用户. 1. Caffe源码:Caffe源 ...
- 浅谈GFC
Web页面的布局,我们常见的主要有“浮动布局(float)”.“定位布局(position)”.“行内块布局(inline-block)”.“CSS3的多栏布局(Columns)”.“伸缩布局(Fle ...
- Win实用好用软件清单推荐
1. 我的Win实用软件清单 排名不分先后且长期更新 有更好用的或者需要帮助的可以留言----最后一次更新于 2019.06.25 1. Dism++ 1.1. 功能: 系统精简.垃圾清理.系统升 ...
- iview“官方“实现的右键菜单
博客开篇,没想到第一篇博文竟然是前端的,虽略显尴尬,但正能量溢出,你我可能遇到同样问题,在这里分享下个人方案,希望对你有用. 官方目前不提供右键菜单,这里借助Dropdown(下拉菜单)来实现,故为“ ...
- 前端安全 xss
整体的 XSS 防范是非常复杂和繁琐的,不仅需要在全部需要转义的位置,对数据进行对应的转义.而且要防止多余和错误的转义,避免正常的用户输入出现乱码. 虽然很难通过技术手段完全避免 XSS,但可以总结以 ...
- POJ P2096 Collecting Bugs
思路 分类讨论,不妨先设$DP[i][j]$表示已经发现$i$种子系统中有$n$种$bug$无非只有四种情况 发现的$bug$在旧的系统旧的分类,概率$p1$是$(i/s)*(j/n)$. 发现的$b ...
- Error:Cannot find bean: "org.apache.struts.taglib.html.BEAN" in any scope
原因:html标签和struts标签混用,或者表单元素外面没有<html:form>标签包裹. 解决:统一标签 <html:form action="/login.do&q ...