题目来源:UVA 624

题目的意思就是:我现在需要从 t 张CD中拿出一部分来,尽可能的凑出接近 N 这么久的音乐,但是不能超过 N。

CD不超过20张,每张长度不超过 N ,不能重复选。

一个很简单的0-1背包。因为最多只有220 = 1048576种可能,所以即使是枚举所有情况都可以毫无压力的搞起。

我这里用的DFS进行枚举,然后得到最好的结果。

最后需要说的是,此题是特判的。输出CD的长度的时候是无序的。

附AC代码:

#include <stdio.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <string>
#include <vector>
namespace std;
  10:  
int t, n, pri[29], res[29], tmp[29], sum = 0;
  12:     
int id)
  14: {
if (id > n)
  16:     {
int m = 0;
int i = 1; i <= n; i++)
if (tmp[i])
  20:                 m += pri[i];
if (m > sum && m <= t)
  22:         {
  23:             sum = m;
int i = 1; i <= n; i++)
  25:                 res[i] = tmp[i];
  26:         }
return;
  28:     }
else
  30:     {
  31:         tmp[id] = 1;
  32:         dp(id+1);
  33:         tmp[id] = 0;
  34:         dp(id+1);
return;
  36:     }
  37: }
  38:  
int main()
  40: {
, &t, &n))
  42:     {
sizeof(res));
sizeof(tmp));
sizeof(pri));
  46:         sum = 0;
int i = 1; i <= n; i++)
, &pri[i]);
  49:         dfs(1);
int i = 1; i <= n; i++)
if (res[i])
, pri[i]);
, sum);
  54:     }
  55: }

相关文章:

  • 2022-12-23
  • 2021-10-01
  • 2022-12-23
  • 2021-04-06
  • 2022-12-23
  • 2022-12-23
  • 2021-12-17
  • 2022-02-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-17
  • 2022-02-06
相关资源
相似解决方案