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),其中 ...
随机推荐
- day20-面向对象基础
目录 面向对象基础 面向过程编程与面向对象编程 面向过程编程 面向对象编程 类与对象 类 对象 定义类和对象 定制对象独有特征 对象属性查找顺序 类与对象的绑定方法 类与数据类型 对象的高度整合 面向 ...
- 3D赛瓦号——整装待发!
随着岁末将至,twaver开发团队依旧马不停蹄,3d产品功能持续更新,新特效和功能目不暇接.现在,我们就利用一些新功能,制作一个全新“赛瓦号”飞船,大家看一下仿真程度是否有质的不同? 网页3d技术正在 ...
- Python学习:ModuleNotFoundError: No module named 'pygal.i18n' 的解决方法
最近在学<Python编程:从入门到实践>,16.2小结中 from pygal.i18n import COUNTRIES 获取两个字母的国别码,我用的pygal的版本是2.4.0(终端 ...
- 用Python实现阿里钉钉机器人读取数据库内容自动发群通知
最近想把一些预警数据信息按照一定的要求自动发送到移动端APP,最终把目标放在了腾讯的微信和阿里的钉钉软件上,由于刚开始学习python,于是编程工具想用python来实现.微信使用群体最广,通过一天的 ...
- outflow Boundary Condition in FLuent
assumption: flow is imcompressible, fully developed, $\partial \phi / \partial X =0$, where is X is ...
- led1,1s取反,led2计数10次取反
1 //利用定时器0 1s,led1取反,利用计数器1,跳10,取反 #include<reg52.h> #define uchar unsigned char #define uint ...
- react入门--------安装react
创建一个单页面应用 Create React App是开始构建新的React单页应用程序的最佳方式. 它可以帮助您快速集成您的开发环境,以便您可以使用最新的JavaScript功能,它提供了一个很好的 ...
- 腾讯云:iptables基础
iptables 基础 iptables 基本命令 任务时间:5min ~ 10min iptables 可以简单理解为 Linux 系统内核级防火墙 netfilter 的用户态客户端. Linux ...
- 洛谷 1894 [USACO4.2]完美的牛栏The Perfect Stall
[题解] 其实是个二分图最大匹配的模板题,直接上匈牙利算法就好了. #include<cstdio> #include<algorithm> #define N 1010 #d ...
- Bazinga HDU 5510 Bazinga(双指针)
Bazinga HDU 5510 Bazinga(双指针) 题链 解法:对于串i来说,如果串i是不符合的,那么代表串i之前的字符串都是i的子串,那么我们求一个新的i(定义为ti),如果i是ti 的子串 ...