题意大概是:给出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. 配置hibernate数据库连接

    第一步:右键项目->MyEclipse->添加Hibernate组件,指定数据库连接配置如下(src/hibernate.cfg.xml) MySQL对连接的有效期是28800s,一个连接 ...

  2. linux使用技巧(shell/vi/screen)

    1,Shell bash > awk '{print {NF}}' file 此时想修改操作命令可参照下面快捷方式 ctrl a 光标移动到命令最前面 ctrl e 光标移动到命令最后面 ctr ...

  3. C# - String与StringBuilder

    A String object is called immutable (read-only), because its value cannot be modified after it has b ...

  4. adb shell top

    PID:进程在系统中的ID CPU% - 当前瞬时所以使用CPU占用率 #THR - 程序当前所用的线程数 UID - 运行当前进程的用户id Name - 程序名称android.process.m ...

  5. python命令行参数处理

    使用argparse包来解析命令行参数: #/usr/bin/python #encoding=utf-8 import argparse parser = argparse.ArgumentPars ...

  6. 猪猪的机器学习笔记(十四)EM算法

    EM算法 作者:樱花猪   摘要: 本文为七月算法(julyedu.com)12月机器学习第十次次课在线笔记.EM算法全称为Expectation Maximization Algorithm,既最大 ...

  7. 初探响应式Web设计

    公司书柜有本<响应式Web设计:HTML5和CSS3实战>,大概就认真看了前面几章,后面大部分介绍css3(随便找本手册都可以了要你可用!) <响应式Web设计:HTML5和CSS3 ...

  8. typeof操作符的返回值

    使用typeof操作符 对一个值使用typeof操作符可能返回下列某个字符串: 1):undefined——如果这个值未定义 2):boolean——如果这个值是布尔值 3):string——如果这个 ...

  9. Python 模块(八) socketserver 以及 线程、进程

    目录 异常处理 socketserver 线程.进程 一.异常处理 try的工作原理是,当开始一个try语句后,python就在当前程序的上下文中作标记,这样当异常出现时就可以回到这里,try子句先执 ...

  10. Introduction to Json

    什么是Json 是Javascript·对象的一种表示,属于轻量级数据,它比XMl小,快,易解析 作用: 用于存储和交换(转换)信息的语言,还可以将各种数据类型放在json中并进行数据传输 整理的章节 ...