购买巧克力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. Python3解leetcode Best Time to Buy and Sell Stock II

    问题描述: Say you have an array for which the ith element is the price of a given stock on day i. Design ...

  2. UDK游戏打包详解

    转自:http://blog.sina.com.cn/s/blog_944177030100ycki.html 安装完的udk目录下有4个主要的文件夹 Binaries -这个文件夹包含游戏的exe程 ...

  3. XCode工程中 Project 和 Targets区别

    转自:http://blog.csdn.net/zhaozy55555/article/details/8557175 project就是一个项目,或者说工程,一个project可以对应多个targe ...

  4. WPF学习系列之四(WPF事件5大类)

    WPF最重要的5类事件: 生命周期事件:这些事件将在元素被初始化,加载或卸载时发生. 鼠标事件 这些事件是鼠标动作的结果. 键盘事件 这些事件是键盘动作的结果. 手写笔事件 这些事件是作用类似铅笔的手 ...

  5. opencvsharp BitmapSource图片截取问题

    private BitmapSource GetUiImage(FrameworkElement ui) { RenderTargetBitmap bmp=new RenderTargetBitmap ...

  6. excel批量提取网页标题

    最近时间比较忙,有时候很多网页需要临时保存,以便空闲的时候查看.单纯的保存网页链接会让人很枯燥,所以需要自动批量提取标题. 为了这个小功能去写个小程序有点不划算,所以就利用excel实现了这个功能. ...

  7. HDU - 6344 2018百度之星资格赛 1001调查问卷(状压dp)

    调查问卷  Accepts: 1289  Submissions: 5642  Time Limit: 6500/6000 MS (Java/Others)  Memory Limit: 262144 ...

  8. 防止APP退到被安卓系统清理

    一个是尽量提高APP权限,无非就是保持APP始终界面在前台 二是使用守护进程方法,被清理了立刻自己启动, 三是前台跟后台进程分开,被重启了恢复原始环境. // 申请设备电源锁,在服务start的时候. ...

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

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

  10. MATLAB求解线性规划