当前位置:网站首页>[usaco06dec]the fewest coins g (hybrid backpack)
[usaco06dec]the fewest coins g (hybrid backpack)
2022-07-19 06:07:00 【lovesickman】
[USACO06DEC]The Fewest Coins G
Title Description
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).
Farmer John I want to buy some supplies in town . In order to accomplish the task efficiently , He wanted to minimize the number of coin changes . Even if he delivers hard The number of coins and change is the least .
John Want to buy something worth T Things that are . Yes N(1<=n<=100) There are two kinds of money in circulation , The denominations are V1,V2…Vn (1<=Vi<=120).John Yes Ci The denomination is Vi The coin of (0<=Ci<=10000).
Let's assume that the owner has an unlimited number of coins , And always change according to the best plan . Note that there is no solution output -1.
Input format
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
Output format
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.
Examples #1
The sample input #1
3 70
5 25 50
5 2 1
Sample output #1
3
Tips
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.
Obviously, the purchase is a multiple backpack ( Binary optimization and above ), Change is a complete backpack , The only problem is to determine the biggest purchase M M M .
Explanation of the problem M = m a x ( v [ i ] 2 ) M = max(v[i]^2) M=max(v[i]2) I won't prove , I didn't understand , however , M = ∑ v [ i ] M = \sum v[i] M=∑v[i] Is clearly established , Then run an enumeration .
/* 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] It means to buy j The minimum number of coins needed to pay for a value item , Multiple backpack
memset(dp,0x3f,sizeof dp); // Completely backpack
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 It's money
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;
}
边栏推荐
- Lithium battery charging management chip
- c语言调用文件浏览器,实现选择文件的效果
- Speed sensor signal isolation, acquisition and transformation, sine wave and sawtooth wave signal input, square wave signal output, signal converter
- Hra2460d-2w high voltage power supply high voltage module - high voltage - high precision hra2460d-2w
- Basic mathematics course 2_ Euler function, linear sieve, extended Euler
- Configure the 'log' shortcut key in vscode and remove the console log(‘‘); Semicolon in;
- Acwing game 58 (AK)
- 2021-09-15
- c语言 指定日期开始多少天 显示
- Fs68001 wireless charging SOC chip has simple periphery and schematic diagram of 5W wireless charging scheme
猜你喜欢

Complete scheme diagram of lth7 five pin chip fs4054 charging circuit principle

【力扣】用栈实现队列
![Chrome browser settings [display the translation language icon in the upper right corner]](/img/87/018e0ab92f698b77ada98ef555bba8.png)
Chrome browser settings [display the translation language icon in the upper right corner]

RestClient-多条件聚合

Configure the 'log' shortcut key in vscode and remove the console log(‘‘); Semicolon in;
![[simple and fast] after startup, the desktop is normal, and the taskbar below is unresponsive / the mouse keeps turning](/img/65/fbb975491d4abd5d1babdf000513e2.png)
[simple and fast] after startup, the desktop is normal, and the taskbar below is unresponsive / the mouse keeps turning

Introduction to easydarawin streaming media server

2021-09-15

Fs4061a (5V USB input, double lithium battery series application, 5V boost charging, 8.4v Management IC

配置VsCode中‘log’快捷键去掉console.log(‘‘);中的分号;
随机推荐
Baby Ehab Partitions Again(dp,构造,位运算)
Introduction to easydarawin streaming media server
Vscode instant English translation plug-in [translation (English Chinese Dictionary)]
EasyDarawin流媒体服务器介绍
go语言介绍及应用场景分析
It4058a single lithium ion battery charging management
minio安装部署及简单使用
Longest bracket match (linear DP)
处理中文分词 ik分词器以及拓展和停止字典
HRA 1~12w series 12V wide voltage 9~18v to ± 250VDC boost converter
vscode 使用技巧1
设置索引库结构,给用户添加可自动补全的suggestion,并将一些字段变成集合放到suggestion里面去
ES聚合分析报错:“reason“ : “Text fields are not optimised for operations
golang 多项目工作区搭建
vscode 配置golang开发环境
[BJOI2019] 排兵布阵(分组背包)
2022/07/09 第五小组 丁帅 学习笔记 day02
5-17陕西科技大学的隐藏学生服务
【牛客】二叉树遍历
【力扣】括号匹配