洛谷 P2983 [USACO10FEB]购买巧克力Chocolate Buying
购买巧克力Chocolate Buying
乍一看以为是背包,然后交了一个感觉没错的背包上去。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
//Mystery_Sky
//
#define M 10000000
#define ll long long
ll f[M];
ll p[M], num[M], sum;
ll n, v;
int main() {
scanf("%lld%lld", &n, &v);
for(int i = 1; i <= n; i++) scanf("%lld%lld", &p[i], &num[i]);
for(int i = 1; i <= n; i++) {
for(ll k = 1; k <= num[i]; k++) {
for(ll j = v; j >= p[i]; j--) {
f[j] = max(f[j], f[j-p[i]]+1);
}
}
}
printf("%lld\n", f[v]);
return 0;
}
结果无情30分。
看了一下数据范围,再仔细想了下,发现不是dp,贪心就可以了,从小到大排序费用,再从小到大买,到买不起为止即可。
Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
//Mystery_Sky
//
#define ll long long
#define M 1000000
struct node {https://i.cnblogs.com/EditCategories.aspx?catid=1
ll p, c;
}m[M];
ll ans, b;
int n;
inline bool cmp(node a, node b)
{
return a.p < b.p;
}
int main() {
scanf("%d %lld", &n, &b);
for(int i = 1; i <= n; i++) scanf("%lld%lld", &m[i].p, &m[i].c);
sort(m+1, m+1+n, cmp);
for(int i = 1; i <= n; i++) {
if(b / m[i].p >= m[i].c) {
ans += m[i].c;
b -= m[i].p * m[i].c;
}
else {
ans += b / m[i].p;
break;
}
}
printf("%lld\n", ans);
return 0;
}
洛谷 P2983 [USACO10FEB]购买巧克力Chocolate Buying的更多相关文章
- 洛谷——P2983 [USACO10FEB]购买巧克力Chocolate Buying
P2983 [USACO10FEB]购买巧克力Chocolate Buying 题目描述 Bessie and the herd love chocolate so Farmer John is bu ...
- 洛谷 P2983 [USACO10FEB]购买巧克力Chocolate Buying 题解
P2983 [USACO10FEB]购买巧克力Chocolate Buying 题目描述 Bessie and the herd love chocolate so Farmer John is bu ...
- 洛谷P2983 [USACO10FEB]购买巧克力Chocolate Buying
题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...
- 洛谷—— P2983 [USACO10FEB]购买巧克力Chocolate Buying
https://www.luogu.org/problem/show?pid=2983 题目描述 Bessie and the herd love chocolate so Farmer John i ...
- 【洛谷】P2983 [USACO10FEB]购买巧克力Chocolate Buying(贪心)
题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...
- P2983 [USACO10FEB]购买巧克力Chocolate Buying
题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...
- [USACO10FEB]购买巧克力Chocolate Buying
题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...
- [USACO10FEB]购买巧克力Chocolate Buying 【假背包真贪心】 By cellur925
题目传送门 继续dp刷题计划,看到这道题,第一眼感觉不就是显然的完全背包嘛.把背包打完要开始填充数组大小的时候成为了mengbier,发现数据极大,达到了1e18.显然这不是一道平凡的背包题目. 于是 ...
- 洛谷 P2984 [USACO10FEB]给巧克力Chocolate Giving
题目描述 Farmer John is distributing chocolates at the barn for Valentine's day, and B (1 <= B <= ...
随机推荐
- C# 性能分析工具
http://msdn.microsoft.com/zh-cn/vstudio/aa497289(en-us).aspx Performance This section includes infor ...
- Python之常用模块(一)
time & datatime 模块 random os sys shutil json & picle time & datetime 时间戳(1970年1月1日之后 ...
- xgene:之ROC曲线、ctDNA、small-RNA seq、甲基化seq、单细胞DNA, mRNA
灵敏度高 == 假阴性率低,即漏检率低,即有病人却没有发现出来的概率低. 用于判断:有一部分人患有一种疾病,某种检验方法可以在人群中检出多少个病人来. 特异性高 == 假阳性率低,即错把健康判定为病人 ...
- 远程访问Linux系统桌面
让Windows可以远程访问Linux系统桌面 http://jingyan.baidu.com/article/d8072ac47b810eec95cefde8.html linux系统下,11款 ...
- Python3 编译中文字串报错解决方案
问题: Python3.6.5 版本中,程序有中文,运行时出现以下error: SyntaxError: Non-UTF-8 code starting with '\xb2' in file XXX ...
- Windows Error Codes
http://www.briandunning.com/error-codes/?source=Windows Windows Error Codes List All Error Codes | S ...
- vs2013代码模板设置
模板设置是为了在“添加新项”时默认格式 1.打开文件:D:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTempl ...
- 【Java】NIO中Channel的注册源码分析
Channel的注册是在SelectableChannel中定义的: public abstract SelectionKey register(Selector sel, int ops, Obje ...
- 51nod1065(set.upper_bound()/sort)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1065 题意:中文题诶- 思路: 解法1:set容器,将所有前 ...
- bzoj 2632: [neerc2011]Gcd guessing game【贪心】
这个告诉gcd的操作实际上就是告诉一个因数是否选,最坏情况是1,判断掉所有因数才能选 然后肯定是用猜不重复质数积比较划算,问题就变成若干个质数,分成数量尽量小每组乘积<=n的若干组 从大质数开始 ...