题意大概是:给出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. [cocos2d-x]用CCSpriteBatchNode进行文理贴图的优化

    引言: 我们在进行手机游戏开发的过程中,由于手机的内存资源是有限的,那么对纹理贴图的优化是非常有必要的,有可能相同的功能,优化的好与不好对内存资源的消耗是非常明显的,下面我就用一个例子来说明一下. 说 ...

  2. 【科研论文】基于文件解析的飞行器模拟系统软件设计(应用W5300)

    摘要: 飞行器模拟系统是复杂飞行器研制和使用过程中的重要设备,它可以用来模拟真实飞行器的输入输出接口,产生与真实系统一致的模拟数据,从而有效避免因使用真实飞行器带来的高风险,极大提高地面测发控系统的研 ...

  3. javaweb学习路之一--web项目搭建

    概述: 工作闲暇时间想要自己搭建一个web项目玩玩,没想到大半天才弄了一个springMVC+mybatis的网站,简直菜的不行,以下记录所有的步骤加深印象 使用环境 1.jdk1.8 2.maven ...

  4. 文件下载 .net

    protected void Button1_Click(object sender, EventArgs e)  { /* 微软为Response对象提供了一个新的方法TransmitFile来解决 ...

  5. SAP HANA 开发者中心(Developer Center)入门指南

  6. scanf 与 cin 的区别

    在论坛上看到有人提出一个如下的问题,在此总结一下. 原问题: http://topic.csdn.net/u/20110414/22/90d0606c-9876-48e4-9b69-bd8bd8a41 ...

  7. Arrays类学习笔记

    Arrays.asList(arr); 该方法可以把数组变成List集合. String[] arr = {"abc","cc"}; List<Strin ...

  8. Spring配置文件模板

    模板: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://ww ...

  9. shell 脚本中$$,$#,$?分别代表什么意思?

    $0 这个程式的执行名字$n 这个程式的第n个参数值,n=1..9$* 这个程式的所有参数,此选项参数可超过9个.$# 这个程式的参数个数$$ 这个程式的PID(脚本运行的当前进程ID号)$! 执行上 ...

  10. c++设计模式总结 好久没写博客了 实在是忙

    具体代码就不贴出来了   通俗易懂的理解方式      原创 c++设计模式: 简单工厂模式 工厂模式有一种非常形象的描述,建立对象的类就如一个工厂,而需要被建立的对象就是一个个产品:在工厂中加工产品 ...