洛谷 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 <= ...
随机推荐
- KCF+Opencv3.0+Cmake+Win10 测试
配置 需要的文件下载 安装CMake,安装opencv3.0.0 在KCFcpp-master 目录下新建一个文件夹,命名为build 打开CMake-GUI配置如下: 点击Configure,编译器 ...
- cocos2dx 3.0 用ClippingNode做游戏的新手引导
转自:http://blog.csdn.net/star530/article/details/20851263 本篇介绍的是用ClippingNode 做游戏的新手引导,额,或者说是做新手引导的一种 ...
- 一 Kubernetes介绍
Kubenetes是一款由Google开发的开源的容器编排工具,它可以解决以下分布式环境下的问题: 调度 你已经得到了这个很棒的基于容器的应用程序? 太棒了!现在你需要确保它能够运行在它应该运行的地方 ...
- Python3中内置类型bytes和str用法及byte和string之间各种编码转换,python--列表,元组,字符串互相转换
Python3中内置类型bytes和str用法及byte和string之间各种编码转换 python--列表,元组,字符串互相转换 列表,元组和字符串python中有三个内建函数:,他们之间的互相转换 ...
- 搭建Android 开发环境(精华)
http://www.cnblogs.com/xdp-gacl/p/4322165.html 孤傲苍狼 只为成功找方法,不为失败找借口! Android开发学习总结(一)——搭建最新版本的Androi ...
- 关于java基础中,接口里面父类的对象指向子类的引用
父类的引用指向子类的对象,它只能看的到父类的那些方法~ 子类自身的方法看不到-- ······························· 如: interface Singer { //定义了 ...
- Linux命令总结_sort排序命令
1.sort命令是帮我们依据不同的数据类型进行排序,其语法及常用参数格式: sort [-bcfMnrtk][源文件][-o 输出文件] 补充说明:sort可针对文本文件的内容,以行为单位 ...
- how to run faster
题目大意: 已知 $$ b_i = \sum_{j=1}^n {(i,j)^d [i,j]^c x_j}$$,给定 $b_i$ 求解 $x_i$ 解法: 考虑 $f(n) = \sum_{d|n}{f ...
- Java异常控制机制和异常处理原则【转】
原文:https://www.jianshu.com/p/15872cba211d Java异常控制机制又被称为“违例控制机制”. 捕获程序错误最理想的时机是在编译阶段,这样可以彻底避免错误的代码运行 ...
- JAVA基础学习-集合三-Map、HashMap,TreeMap与常用API
森林森 一份耕耘,一份收获 博客园 首页 新随笔 联系 管理 订阅 随笔- 397 文章- 0 评论- 78 JAVA基础学习day16--集合三-Map.HashMap,TreeMap与常用A ...