专栏首页数据结构与算法洛谷P2851 [USACO06DEC]最少的硬币The Fewest Coins(完全背包+多重背包)

洛谷P2851 [USACO06DEC]最少的硬币The Fewest Coins(完全背包+多重背包)

题目描述

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(1<=T<=10000)样东西(2017-7-20 管理员注:这个翻译有问题,实际为要买价值为T的东西)。有N(1<=n<=100)种货币参与流通,面值分别为V1,V2..Vn (1<=Vi<=120)。John有Ci个面值为Vi的硬币(0<=Ci<=10000)。我们假设店主有无限多的硬币, 并总按最优方案找零。

输入输出格式

输入格式:

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:

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.

思路比较简单

对john做一次多重背包

对店主做一次完全背包(然而不会写代码)

多重背包用二进制优化

另外,本蒟蒻不怎么懂为什么枚举上界是所有面值相乘再加T,

刚开始写面值乘数量死活RE QWQ...

#include<cstring>
#include<cstdio>
#include<cstdlib>
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<22,stdin),p1==p2)?EOF:*p1++)
#define min(a,b) (a<b?a:b)
#define max(a,b) (a<b?b:a)
char buf[1<<22],*p1=buf,*p2=buf;
//#define int long long 
using namespace std;
const int MAXN=5*1e6+10,INF=1e8+10;
inline int read() {
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
int f[MAXN];//恰好为i时的最小花费 
int g[MAXN];//完全背包 
int val[MAXN],num[MAXN];
int N,T,limit=0;
int main() {
    #ifdef WIN32
    freopen("a.in","r",stdin);
    #endif
    N=read();T=read();
    for(int i=1;i<=N;i++) val[i]=read();
    for(int i=1;i<=N;i++) num[i]=read(),limit+=val[i]*val[i];
    memset(f,0x3f,sizeof(f));
    memset(g,0x3f,sizeof(g));
    g[0]=0;f[0]=0;
    for(int i=1;i<=N;i++)
        for(int j=val[i];j<=limit;j++)
            g[j]=min(g[j],g[j-val[i]]+1);
    for(int i=1;i<=N;i++) {
        for(int k=1;k<=num[i];k<<=1) {
            for(int j=limit;j>=val[i]*k;j--)
                    f[j]=min(f[j],f[j - val[i]*k]+k);
            num[i]-=k;
        }        
        if(num[i])
            for(int j=limit;j>=val[i]*num[i];j--) 
                f[j]=min(f[j],f[j - val[i]*num[i]]+num[i]);    
    }
    int ans=INF;
    for(int i=T;i<=limit;i++)
        ans=min(ans,f[i]+g[i-T]);
    ans==INF?printf("-1"):printf("%d",ans);
    return 0;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Kosaraju算法详解

    Kosaraju算法可以求出有向图中的强连通分量个数,并且对分属于不同强连通分量的点进行标记。它的算法描述较为简单: (1) 第一次对图G进行DFS遍历,并在...

    attack
  • BZOJ 5248: [2018多省省队联测]一双木棋(对抗搜索)

    attack
  • ZR国庆Round2解题报告

    然后刚T3暴力,刚完还有2h左右。。然后,,这时候我zz的选择去打T2的暴力,然而T2暴力真的不是一般的难写。。

    attack
  • 算法提高 金陵十三钗

    金陵十三钗   本题难度:难   本题占分比例:5% 问题描述   在电影《金陵十三钗》中有十二个秦淮河的女人要自我牺牲代替十二个女学生去赴日本...

    AI那点小事
  • HDUOJ --2544最短路(基础)

    输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛...

    Gxjun
  • 古典密码加密解密之多表代换

    多表代换密码首先将明文M 分为由n 个字母组成的分组, , … ,对每个分组的加密为 ≡ + ( ), = , , … 其中,(A,B)是密钥,A 是 ...

    张泽旭
  • Golang Leetcode 977. Squares of a Sorted Array.go

    更多内容请移步我的repo:https://github.com/anakin/golang-leetcode

    anakinsun
  • HDU 1083 Courses(二分图最大匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1083

    Ch_Zaqdt
  • LintCode整数排序题目分析解答选择排序插入排序小结

    Given an integer array, sort it in ascending order. Use selection sort, bubble s...

    desperate633
  • 蓝桥杯vip测试题系统试题-算法提高 矩阵转置

    给定一个n×m矩阵相乘,求它的转置。其中1≤n≤20,1≤m≤20,矩阵中的每个元素都在整数类型(4字节)的表示范围内。

    TrueDei

扫码关注云+社区

领取腾讯云代金券