首页 > 其他分享 >HDU - 2844 - coins

HDU - 2844 - coins

时间:2023-09-05 10:55:39浏览次数:43  
标签:HDU 背包 int 2844 coins dp

HDU - 2844 - coins (多重背包)

题意:

大壮想买东西,他有n种不同面值的硬币,每种有 $c_i$ 个,他不想找零,也不想买超过价值m的东西,问他有多少种支付方式。$n(1 ≤ n ≤ 100),m(m ≤ 100000)$

分析:

可以发现m的范围不大,直接在m中遍历。转化为给定一个容量为m的背包,问装入不同方案时,不同价值的数量。

显然是一个多重背包,需要使用二进制优化,标记出现过的不同价值即可。

为什么呢?因为求的是m中出现过的价值,那么如果一个价值可以被凑出来,假设为x,那么当背包容量刚好为x时,一定没有比它更优的方案。

const int N = 110;
const int M = 1e5 + 10;
void solve() {
    int n, m;
    while (cin >> n >> m && n != 0 && m != 0) {
        memset(dp, 0, sizeof dp);
        for (int i = 1; i <= n; i++)cin >> a[i];
        int pos = 0;
        for (int i = 1; i <= n; i++) {
            int x; cin >> x;
            int k = 1;
            while (k <= x) {
                if (pos > m)break;
                v[++pos] = a[i] * k;
                x -= k;
                k <<= 1;
            }
            if (x && pos < m) v[++pos] = a[i] * x;
        }
        for (int i = 1; i <= pos; i++) {
            for (int j = m; j >= v[i]; j--) {
                dp[j] = max(dp[j], dp[j - v[i]] + v[i]);
                if (dp[j] > 0)vi[dp[j]]++;
            }
        }
        int ans = 0;
        for (int i = 1; i <= m; i++) {
            if (vi[i])ans++;
        }
        cout << ans << endl;
        memset(vi, 0, sizeof vi);
    }
}

题目链接:Problem - 2844 (hdu.edu.cn)

标签:HDU,背包,int,2844,coins,dp
From: https://www.cnblogs.com/yxyxyxyxyx/p/17679072.html

相关文章

  • 代码(待加解释) hdu2196
    #include<bits/stdc++.h>usingnamespacestd;constintmaxn=3e4+10;#definelllonglonginthead[maxn],ver[maxn],nxt[maxn],edge[maxn];inttot;llf[maxn][3];intrx[maxn];voiddfs1(intx,intfa){  for(inti=head[x];i;i=nxt[i])  {   ......
  • hdu:Machine Schedule(二分图匹配)
    ProblemDescriptionAsweallknow,machineschedulingisaveryclassicalproblemincomputerscienceandhasbeenstudiedforaverylonghistory.Schedulingproblemsdifferwidelyinthenatureoftheconstraintsthatmustbesatisfiedandthetypeof......
  • hdu:Oil Deposits(dfs连通图)
    ProblemDescriptionTheGeoSurvCompgeologicsurveycompanyisresponsiblefordetectingundergroundoildeposits.GeoSurvCompworkswithonelargerectangularregionoflandatatime,andcreatesagridthatdividesthelandintonumeroussquareplots.......
  • hdu:手机的诱惑(dfs+剪枝)
    ProblemDescription张晨乐在一个古老的迷宫中发现了一个手机,这个手机深深地吸引了他。然而,当他拾起手机,迷宫开始摇晃,张晨乐能感觉到地面下沉。他意识到:这个手机只是一个诱饵!于是,他不顾一切地试图冲出这个迷宫。迷宫是一个大小为N*M的矩形,有一扇门,一开始,门是关闭的,并在第T秒打......
  • hdu:Rescue(bfs+优先队列)
    ProblemDescriptionAngelwascaughtbytheMOLIGPY!HewasputinprisonbyMoligpy.TheprisonisdescribedasaN*M(N,M<=200)matrix.ThereareWALLs,ROADs,andGUARDsintheprison.Angel’sfriendswanttosaveAngel.Theirtaskis:approach......
  • hdu:Knight Moves(bfs)
    ProblemDescriptionAfriendofyouisdoingresearchontheTravelingKnightProblem(TKP)whereyouaretofindtheshortestclosedtourofknightmovesthatvisitseachsquareofagivensetofnsquaresonachessboardexactlyonce.Hethinksthatthe......
  • hdu:A strange lift(bfs)
    ProblemDescriptionThereisastrangelift.Theliftcanstopcanateveryfloorasyouwant,andthereisanumberKi(0<=Ki<=N)oneveryfloor.Thelifthavejusttwobuttons:upanddown.Whenyouatfloori,ifyoupressthebutton“UP”,youwi......
  • hdu:一个人的旅行
    ProblemDescription虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中会遇见很多人(白马王子,0),很多事,还能丰富自己的阅历,还可以看美丽的风景……草儿想去很多地方,她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋,去纽......
  • hdu:悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
    ProblemDescription急!灾区的食物依然短缺!为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品,其价格不等,并且只能整袋购买。请问:你用有限的资金最多能采购多少公斤粮食呢?后记:人生是一个充满了......
  • hdu:Piggy-Bank(背包)
    ProblemDescriptionBeforeACMcandoanything,abudgetmustbepreparedandthenecessaryfinancialsupportobtained.ThemainincomeforthisactioncomesfromIrreversiblyBoundMoney(IBM).Theideabehindissimple.WheneversomeACMmemberhasany......