购买巧克力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的更多相关文章

  1. 洛谷——P2983 [USACO10FEB]购买巧克力Chocolate Buying

    P2983 [USACO10FEB]购买巧克力Chocolate Buying 题目描述 Bessie and the herd love chocolate so Farmer John is bu ...

  2. 洛谷 P2983 [USACO10FEB]购买巧克力Chocolate Buying 题解

    P2983 [USACO10FEB]购买巧克力Chocolate Buying 题目描述 Bessie and the herd love chocolate so Farmer John is bu ...

  3. 洛谷P2983 [USACO10FEB]购买巧克力Chocolate Buying

    题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...

  4. 洛谷—— P2983 [USACO10FEB]购买巧克力Chocolate Buying

    https://www.luogu.org/problem/show?pid=2983 题目描述 Bessie and the herd love chocolate so Farmer John i ...

  5. 【洛谷】P2983 [USACO10FEB]购买巧克力Chocolate Buying(贪心)

    题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...

  6. P2983 [USACO10FEB]购买巧克力Chocolate Buying

    题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...

  7. [USACO10FEB]购买巧克力Chocolate Buying

    题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...

  8. [USACO10FEB]购买巧克力Chocolate Buying 【假背包真贪心】 By cellur925

    题目传送门 继续dp刷题计划,看到这道题,第一眼感觉不就是显然的完全背包嘛.把背包打完要开始填充数组大小的时候成为了mengbier,发现数据极大,达到了1e18.显然这不是一道平凡的背包题目. 于是 ...

  9. 洛谷 P2984 [USACO10FEB]给巧克力Chocolate Giving

    题目描述 Farmer John is distributing chocolates at the barn for Valentine's day, and B (1 <= B <= ...

随机推荐

  1. 洛谷P3386——二分图匹配

    题目:https://www.luogu.org/problemnew/show/P3386 二分图匹配模板,注意左部点只dfs未匹配点. 代码如下: #include<iostream> ...

  2. POCO库中文编程参考指南(10)如何使用TCPServer框架?

    1 TCPServer 框架概述 POCO 库提供TCPServer框架,用以搭建自定义的 TCP 服务器.TCPServer维护一个连接队列.一个连接线程池.连接线程用于处理连接,连接线程只要一空闲 ...

  3. hadoop--谷歌三大论文

    学习大数据必读的三个论文: http://pan.baidu.com/s/1c0FA69U 在我的网盘,大家可以去下载 Google File System中文版 Google Bigtable中文版 ...

  4. There&nbsp;is&nbsp;no&nbsp;resul…

    There is no result type defined for type 'json' mapped with name 'success'. 这个错误是json初学者很容易遇到的错误:现在把 ...

  5. skb详细解析【转】

    skb详细解析[转]    摘自:http://blog.chinaunix.net/uid-30035229-id-4883992.html     在自己的模块发送函数中,需要对skb进行重新构造 ...

  6. ubuntu 下交叉编译环境的搭建

    1. 安装标准的C开发环境,由于Linux安装默认是不安装的,所以需要先安装一下(如果已经安装好的话,就可以免去这一步了): #sudo apt-get install gcc g++ libgcc1 ...

  7. Python模拟登录代码

    注:访问http://127.0.0.1:8080/user/6,总是会要求必须有登录权限,也就是,若未登录,访问该页面,会跳转到登陆页面. 全自动模拟登录 半自动模拟登录:

  8. 1.4 DVWA亲测文件上传漏洞

    Low 先看看源代码: <?php if(isset( $_POST[ 'Upload' ] ) ) { // Where are we going to be writing to? $tar ...

  9. sqlserver2012——变量declare

    1.声明变量病定义类型 赋值操作 ) set @name='小明' select @name 使用select进行赋值 ) select @name='李明' seelelct @name

  10. 洛谷 - P1063 - 能量项链 - 区间dp

    https://www.luogu.org/problemnew/show/P1063 这个并不是每次只能从两边扩展的,可以从中间断开. #include<bits/stdc++.h> u ...