题意大概是:给出N个硬币, 面值为a_i, 问要凑成X元哪些硬币是不可或缺的.1 ≤ N ≤ 200, 1 ≤ x ≤ 10^4

直接枚举, 然后就是01背包了. 为了不让复杂度多乘个N, 我们就从左往右, 从右往左分别dp一次.这样判断一个硬币就是O(X).总时间复杂度O(NX)

-----------------------------------------------------------------------------

#include<bits/stdc++.h>
 
using namespace std;
 
const int maxn = 209;
const int maxv = 10009;
 
bool L[maxn][maxv], R[maxn][maxv];
int v[maxn], N, V;
vector<int> ans;
 
int main() {
memset(L, false, sizeof L);
memset(R, false, sizeof R);
cin >> N >> V;
for(int i = 1; i <= N; i++)
   scanf("%d", v + i);
L[0][0] = R[N + 1][0] = true;
for(int i = 1; i <= N; i++) {
for(int j = 0; j <= V; j++)
   L[i][j] = L[i - 1][j];
for(int j = V; j >= v[i]; j--)
   L[i][j] |= L[i - 1][j - v[i]];
}
for(int i = N; i; i--) {
for(int j = 0; j <= V; j++)
   R[i][j] = R[i + 1][j]; 
for(int j = V; j >= v[i]; j--)
   R[i][j] |= R[i + 1][j - v[i]];
}
for(int i = 1; i <= N; i++) {
bool F = false;
for(int j = 0; j <= V; j++)
   if(L[i - 1][j] && R[i + 1][V - j]) {
   F = true;
   break;
}
if(!F) ans.push_back(v[i]);
}
printf("%d\n", ans.size());
for(int i = 0; i < ans.size(); i++)
   printf("%d ", ans[i]);
return 0;
}

-----------------------------------------------------------------------------

415. Necessary Coins

Time limit per test: 1.25 second(s)
Memory limit: 262144 kilobytes
input: standard
output: standard

Vasya has been on vacation on Mars. He's a big fan of foreign coins, and thus has collected exactly one martian coin of each denomination, for a total of n coins: a1 martian dollars, a2 martian dollars, etc, an martian dollars. Unfortunately, he couldn't stand ordering the Pan Galactic Gargle Blaster at the Starport, and has to pay for it — it costs x martian dollars. Vasya is wondering which of his coins are absolutely necessary to do so (i.e., he is forced to abandon them). They don't offer change at the Starport Mars.

Input

The input file contains two integer numbers n and x (1 ≤ n ≤ 200, 1 ≤ x ≤ 104), followed by n distinct integer numbersai (1 ≤ ai ≤ x).

Output

On the first line of output, print the amount of denominations of coins that appear in any subset that sums to xmartian dollars. On the second line of output, print the denominations themselves, in any order, separated with single spaces. It is guaranteed that there exists at least one way to pay x martian dollars with the given coins.

Example(s)
sample input
sample output
5 18 1 2 3 5 10 
2 5 10 

SGU 415. Necessary Coins ( 背包dp )的更多相关文章

  1. Codechef APRIL14 ANUCBC Cards, bags and coins 背包DP变形

    题目大意 有n个数字,选出一个子集,有q个询问,求子集和模m等于0的方案数%1000000009.(n <= 100000,m <= 100,q <= 30) 假设数据很小,我们完全 ...

  2. PAT甲题题解-1068. Find More Coins (30)-dp,01背包

    一开始没多想,虽然注意到数据N<=10^4的范围,想PAT的应该不会超时吧,就理所当然地用dfs做了,结果最后一组真的超时了.剪枝啥的还是过不了,就意识到肯定不是用dfs做了.直到看到别人说用0 ...

  3. 背包dp整理

    01背包 动态规划是一种高效的算法.在数学和计算机科学中,是一种将复杂问题的分成多个简单的小问题思想 ---- 分而治之.因此我们使用动态规划的时候,原问题必须是重叠的子问题.运用动态规划设计的算法比 ...

  4. hdu 5534 Partial Tree 背包DP

    Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  5. HDU 5501 The Highest Mark 背包dp

    The Highest Mark Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  6. Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp

    B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...

  7. noj [1479] How many (01背包||DP||DFS)

    http://ac.nbutoj.com/Problem/view.xhtml?id=1479 [1479] How many 时间限制: 1000 ms 内存限制: 65535 K 问题描述 The ...

  8. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  9. BZOJ 1004: [HNOI2008]Cards( 置换群 + burnside引理 + 背包dp + 乘法逆元 )

    题意保证了是一个置换群. 根据burnside引理, 答案为Σc(f) / (M+1). c(f)表示置换f的不动点数, 而题目限制了颜色的数量, 所以还得满足题目, 用背包dp来计算.dp(x,i, ...

随机推荐

  1. golang实现tcp接入服务器

    接入服务器和后端业务服务其维持tcp连接,多个前端请求通过接入服务器访问后端业务服务器,接入服务器可以方便增加路由功能,维护多个业务服务器,根据消息ID路由到具体的业务服务器. 项目目录如下 simp ...

  2. windows下使用python googleprotobuf

    首先下载:protobuf-2.5.0.tar.gz 和protoc-2.5.0-win32.zip.两者的版本要对应: 将下载的google protobuf解压,会看到一个python目录,Win ...

  3. js正则表达式验证字符长度

    原理,就是把一个汉字替换为两个字母,来实现长度验证. //js正则验证字符长度 第一种:直接输出长度 alert('1sS#符'.replace(/[^\x00-\xff]/g, 'AA').leng ...

  4. .net EF中从数据添加表或视图时无法添加的问题

    .net 使用EF模式进行开发,添加实体时不能够正常添加 错误描述: .net中在EF文件中添加数据库中已有的表或视图时不能正常添加,在添加时没有任何的错误提示,但是表或视图就一直拉不过来,,保存也没 ...

  5. Piggy-Bank (完全背包)

      Description Before ACM can do anything, a budget must be prepared and the necessary financial supp ...

  6. Java大顶和小顶

    http://blog.sina.com.cn/s/blog_651c9a360100o7y1.html http://blog.csdn.net/cnbird2008/article/details ...

  7. 数据切分——Mysql分区表的管理与维护

    关于Mysql分区表的介绍可以参考: http://blog.csdn.net/jhq0113/article/details/44592865 关于Mysql分区表的创建可以参考: http://b ...

  8. PROPAGATION_REQUIRED

    PROPAGATION_REQUIRED (2009-05-13 13:26:52) 转载▼   事务传播行为种类 Spring在TransactionDefinition接口中规定了7种类型的事务传 ...

  9. IT第二十六天 - Swing、上周总结

    IT第二十六天 上午 Swing 1.对于方法的参数如果是int数值类型,应该直接调用该类中的常量属性,而不应该直接填入数字 2.Toolkit类中定义的方法是可以直接访问本地计算机(操作系统)信息的 ...

  10. 【Web】throw和throws的区别和用法。

    1.throws关键字通常被应用在声明方法时,用来指定可能抛出的异常.多个异常可以使用逗号隔开.当在主函数中调用该方法时,如果发生异常,就会将异常抛给指定异常对象.如下面例子所示: public cl ...