题意大概是:给出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. 【网络协议】TCP中的四大定时器

    前言 对于每个TCP连接,TCP一般要管理4个不同的定时器:重传定时器.坚持定时器.保活定时器.2MSL定时器. 重传定时器 非常明显重传定时器是用来计算TCP报文段的超时重传时间的(至于超时重传时间 ...

  2. UVA 11754 - Code Feat(数论)

    UVA 11754 - Code Feat 题目链接 题意:给定一个c个x, y1,y2,y3..yk形式,前s小的答案满足s % x在集合y1, y2, y3 ... yk中 思路:LRJ大白例题, ...

  3. [置顶] 局部加权回归、最小二乘的概率解释、逻辑斯蒂回归、感知器算法——斯坦福ML公开课笔记3

    转载请注明:http://blog.csdn.net/xinzhangyanxiang/article/details/9113681 最近在看Ng的机器学习公开课,Ng的讲法循循善诱,感觉提高了不少 ...

  4. JavaScript 高级程序设计(第3版)笔记——chapter5:引用类型(基本包装类型部分)

    一.介绍 为了方便操作基本类型值,ECMAScript还提供了3个特殊的引用类型:Boolean, Number, String. 实际上,每当读取一个基本类型值得时候,后台就会创建一个对应的基本包装 ...

  5. 第10季asp.net基础

    什么是ASP.Net: ASP.Net是一种动态网页技术,在服务器端运行.Net代码,动态生成HTML.可以使用javascript.Dom在浏览器端完成很多工作,但是有很多工作无法在浏览器端完成,比 ...

  6. ubuntu中彻底删除nginx

    1.先执行一下命令: 1.1 删除nginx,–purge包括配置文件 sudo apt-get --purge remove nginx 1.2 自动移除全部不使用的软件包 sudo apt-get ...

  7. Linux解压缩总结

    看文件名的后缀名,不同的后缀的文件解压和压缩的命令都不一样总结如下: *.tar 用 tar –xvf 解压 *.gz 用 gzip -d或者gunzip 解压 *.tar.gz和*.tgz 用 ta ...

  8. C# Programming Study #1

    引用的时候需要在参数和使用的时候加上 ref 关键字 static bool addnum (ref int val) //引用 { ++val; return true; } 参数数组的概念,可以接 ...

  9. 数组排序-Objectivec

    发表于昨天(23:33)(2013-11-03 23:33) ,已有15次阅读 ,共0个评论 摘要: 总结OC中数组排序3种方法:sortedArrayUsingSelector:;sortedArr ...

  10. String详解说明

    大家平时都string都不是很在意,但是每当面试碰到String的时候在“==”和equals之间就乱了,下面我来说一说String,也许不够全面,请大家多多指教,希望会帮到处于蒙圈状态的人们. 一. ...