[USACO06DEC]The Fewest Coins G

题目描述

Farmer John has gone to town to buy some farm supplies. Being a very efficient man, he always pays for his goods in such a way that the smallest number of coins changes hands, i.e., the number of coins he uses to pay plus the number of coins he receives in change is minimized. Help him to determine what this minimum number is.

FJ wants to buy T (1 ≤ T ≤ 10,000) cents of supplies. The currency system has N (1 ≤ N ≤ 100) different coins, with values V1, V2, …, VN (1 ≤ Vi ≤ 120). Farmer John is carrying C1 coins of value V1, C2 coins of value V2, …, and CN coins of value VN (0 ≤ Ci ≤ 10,000). The shopkeeper has an unlimited supply of all the coins, and always makes change in the most efficient manner (although Farmer John must be sure to pay in a way that makes it possible to make the correct change).

农夫John想到镇上买些补给。为了高效地完成任务,他想使硬币的转手次数最少。即使他交付的硬 币数与找零得到的的硬币数最少。

John想要买价值为T的东西。有N(1<=n<=100)种货币参与流通,面值分别为V1,V2…Vn (1<=Vi<=120)。John有Ci个面值为Vi的硬币(0<=Ci<=10000)。

我们假设店主有无限多的硬币, 并总按最优方案找零。注意无解输出-1。

输入格式

Line 1: Two space-separated integers: N and T.

Line 2: N space-separated integers, respectively V1, V2, …, VN coins (V1, …VN)

Line 3: N space-separated integers, respectively C1, C2, …, CN

输出格式

Line 1: A line containing a single integer, the minimum number of coins involved in a payment and change-making. If it is impossible for Farmer John to pay and receive exact change, output -1.

样例 #1

样例输入 #1

3 70
5 25 50
5 2 1

样例输出 #1

3

提示

Farmer John pays 75 cents using a 50 cents and a 25 cents coin, and receives a 5 cents coin in change, for a total of 3 coins used in the transaction.


明显购买是一个多重背包(二进制优化及以上),找零是一个完全背包,唯一的问题是确定购买的最大 MMM 。

题解中说 M=max(v[i]2)M = max(v[i]^2)M=max(v[i]2) 我不会证明,也没看懂,不过, M=∑v[i]M = \sum v[i]M=∑v[i] 是明显成立的,然后跑一个枚举就行。

/*
A: 10min
B: 20min
C: 30min
D: 40min
*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <assert.h>
#include <sstream>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo_(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")template<typename T>
ostream& operator<<(ostream& os,const vector<T>&v){for(int i=0,j=0;i<v.size();i++,j++)if(j>=5){j=0;puts("");}else os<<v[i]<<" ";return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>&v){for(auto c:v)os<<c<<" ";return os;}
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const map<T1,T2>&v){for(auto c:v)os<<c.first<<" "<<c.second<<endl;return os;}
template<typename T>inline void rd(T &a) {char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}typedef pair<long long ,long long >PII;
typedef pair<long,long>PLL;typedef long long ll;
typedef unsigned long long ull;
const int N=2e5+10,M=1e9+7;ll n,m;
struct good{int v,w;
};
vector<good>g;
int V[110],C[110],f[21000],dp[21000],mx;
void solve(){cin>>n>>m;memset(f,0x3f,sizeof f); // f[j]表示恰好购买j价值物品所需要的最小支付硬币数量 , 多重背包memset(dp,0x3f,sizeof dp); // 完全背包f[0] = 0;dp[0] = 0;fo(i,1,n)cin>>V[i],mx = max(mx,V[i]);mx *= mx;fo(i,1,n)cin>>C[i];fo(i,1,n){int s = C[i];for(int k=1;k<=C[i];k<<=1){g.pb({k*V[i],k});s-=k;}if(s>0){g.pb({s*V[i],s});}}for(auto c:g){for(int j=mx;j>=c.v;j--){// J 是钱f[j] = min(f[j],f[j-c.v]+c.w);}for(int j=c.v;j<=mx;j++){dp[j] = min(dp[j],dp[j-c.v]+c.w);}}if(f[m] == 0x3f3f3f3f){puts("-1");}else{int ans = 0x3f3f3f3f;for(int i=m;i<=m+mx;i++){debug(i);ans = min(ans,f[i]+dp[i-m]);}if(ans == 0x3f3f3f3f)ans=-1;cout<<ans;}
}
int main(){solve();return 0;
}

[USACO06DEC]The Fewest Coins G(混合背包)相关推荐

  1. 洛谷P2851 [USACO06DEC]The Fewest Coins G 题解

    洛谷P2851 [USACO06DEC]The Fewest Coins G 题解 题目链接:P2851 [USACO06DEC]The Fewest Coins G 题意: Farmer John ...

  2. The Fewest Coins (混合背包)

    有的物品只可以取一次或不取(基本的0-1背包),有的物品可以取无限次(完全背包),有的物品可以取的次数有一个上限(多重背包),就是混合背包问题. The Fewest Coins 题意: 农夫John ...

  3. The Fewest Coins(多重背包+完全背包)

    The Fewest Coins(多重背包+完全背包) Farmer John has gone to town to buy some farm supplies. Being a very eff ...

  4. poj 3260 The Fewest Coins(多重背包+完全背包)

    http://poj.org/gotoproblem?pid=3260 (1)多重背包的处理方式:转化为分组背包(1,2,4,8,余数).具体细节参见代码: scanf("%d", ...

  5. 【题目记录】——POJ 3260 The Fewest Coins 混合背包

    POJ 3260 The Fewest Coins 题目地址[POJ 3260 The Fewest Coins] 题意:John要去买价值为m的商品. 现在的货币系统有n种货币,对应面值为val[1 ...

  6. 动态规划dp(带模板题の超易懂版):01背包,完全背包,分组背包,多重背包,混合背包

    动态规划dp(带模板题の超易懂版):01背包,完全背包,分组背包,多重背包 01背包 && 完全背包 && 分组背包 の 视频教程:https://www.bilibi ...

  7. nvcc gcc g++混合编译器编程

    nvcc gcc g++混合编译器编程 有很多同鞋问怎么使用CUDA和其它的编译器连用呢?混合编程? 先吧代码贴出来: 文件1 : test1.cu [cpp] view plaincopy //文件 ...

  8. Codevs 3269 混合背包

    3269 混合背包 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 背包体积为V ,给出N个物品,每个物品占用体积为Vi,价值 ...

  9. 九十一、动态规划系列 背包问题之混合背包

    @Author:Runsen @Date:2020/09/27 背包系列,是动态规划里一类典型的问题,主要有:01背包,完全背包,多重背包,混合背包,二维费用背包,分组背包,有依赖背包和泛化物品等.也 ...

最新文章

  1. 独家 | 教你用Scrapy建立你自己的数据集(附视频)
  2. 最简单的kafka demo案例
  3. Dungeon Master(三维bfs)
  4. 计算机如何打开无线网络适配器,win7系统下网络适配器打不开怎么解决
  5. linux 安装 maven 、解决:bash: mvn: command not found
  6. PHP array_map()
  7. jvm lock低性能分析
  8. tf卡量产工具万能版_手上还有SD卡/TF卡的小伙伴,这些玩法你有关注过吗
  9. 响应函数sys_xxx
  10. bzoj 3036: 绿豆蛙的归宿(Dp)
  11. Oracle高级查询之GROUP BY
  12. 自动柜员机属于微型计算机的一种,自动柜员机属于微型计算机的一种。(  )...
  13. 大数据与云计算概论简介
  14. 这个开源项目有点强,无需编码,可一键生成前后端代码
  15. ORA-01261: Parameter db_recovery_file_dest destination string cannot be translated
  16. AndroidSDK目录和源码目录详解
  17. WIN10下系统缩放比例(DPI)的魔幻设置
  18. 2023年网络安全比赛--跨站脚本攻击①中职组(超详细)
  19. 【嵌入式流媒体开发】Linux ALSA 声卡数据采集与播放
  20. 电脑qq怎么设置远程桌面连接到服务器,QQ远程协助在哪个位置 qq远程协助如何使用...

热门文章

  1. 【教程】如何利用patchrom来编译你自己的MIUI
  2. POI之Excel单元格样式
  3. 《深度学习从0开始》
  4. 小程序推广换量经验分享
  5. [ICPC Asia Nanjing 2019] Holy Grail (spfa最短路)
  6. meo学习笔记4:C++中对象占用内存情况
  7. 计算机网络技术中的单位换算,计算机存储/网络传输中单位换算1000还是1024
  8. android 游戏循环 帧速,适用于Android的Firemonkey游戏循环
  9. 【自动驾驶感知领域目前研究热点】
  10. Objective-C类别(catagory)