http://codeforces.com/problemset/problem/688/E

题目需要在n个数中找出一个集合,使得这个集合的和为val,然后问这些所有集合,能产生多少个不同的和值。

题解是直接两个同时dp,设dp[j][h]表示主集合的和为j,能否产生h这个数字。

把他们看作是两个集合,对于每个数,都可以放在第一个集合,或者放在第一个集合后,再放入第二个集合。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = + ;
bool dp[maxn][maxn];
set<int>ans;
void work() {
int n, k;
cin >> n >> k;
dp[][] = true;
for (int i = ; i <= n; ++i) {
int x;
cin >> x;
for (int j = k; j >= ; --j) {
for (int h = j; h >= ; --h) {
if (j >= x) { //第一个集合
dp[j][h] = dp[j][h] || dp[j - x][h];
}
// if (h >= x) {
// dp[j][h] = dp[j][h] || dp[j][h - x];
// }
if (h >= x && j >= x) {
dp[j][h] = dp[j][h] || dp[j - x][h - x];
} }
}
}
// cout << dp[5][0] << endl;
for (int i = ; i <= k; ++i) {
if (dp[k][i] == false) continue;
ans.insert(i);
}
cout << ans.size() << endl;
for (set<int> :: iterator it = ans.begin(); it != ans.end(); ++it) {
cout << *it << " ";
}
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

E. The Values You Can Make 背包,同时DP的更多相关文章

  1. 背包&数位dp(8.7)

    背包 0/1背包 设dp[i][j]为前i个物品选了j体积的物品的最大价值/方案数 dp[i][j]=max(dp[i-1][j-w[i]]+v[i],dp[i-1][j])(最大价值) dp[i][ ...

  2. CF687C. The Values You Can Make[背包DP]

    C. The Values You Can Make time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  3. CoderForces 687C The Values You Can Make (01背包,DP)

    题意:给定 n 个硬币和一个值 k,问你在用一些硬币组成面值为 k的这些硬币还能组成多少种其他面值. 析:如果这样说,由这些硬币能组成多少种不同的面值,那么是不是就很熟悉了,这不就是01背包么,这个题 ...

  4. 九度OJ 1123:采药 (01背包、DP、DFS)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2705 解决:1311 题目描述: 辰辰是个很有潜能.天资聪颖的孩子,他的梦想是称为世界上最伟大的医师. 为此,他想拜附近最有威望的医师为师 ...

  5. HDU 2955 Robberies 背包概率DP

    A - Robberies Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submi ...

  6. [CF189A]Cut Ribbon(完全背包,DP)

    题目链接:http://codeforces.com/problemset/problem/189/A 题意:给你长为n的绳子,每次只允许切a,b,c三种长度的段,问最多能切多少段.注意每一段都得是a ...

  7. HDU5800 To My Girlfriend 背包计数dp

    分析:首先定义状态dp[i][j][s1][s2]代表前i个物品中,选若干个物品,总价值为j 其中s1个物品时必选,s2物品必不选的方案数 那么转移的时候可以考虑,第i个物品是可选可可不选的 dp[i ...

  8. hdu 1561 The more, The Better (依赖背包 树形dp)

    题目: 链接:点击打开链接 题意: 非常明显的依赖背包. 思路: dp[i][j]表示以i为根结点时攻击j个城堡得到的最大值.(以i为根的子树选择j个点所能达到的最优值) dp[root][j] = ...

  9. poj 1015 Jury Compromise(背包变形dp)

    In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting of ...

随机推荐

  1. C++第9周(春)项目5 - 一元一次方程类

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目5]设计一元一次方程类.求形如ax+b= ...

  2. HDU 1031.Design T-Shirt【结构体二次排序】【8月21】

    Design T-Shirt Problem Description Soon after he decided to design a T-shirt for our Algorithm Board ...

  3. VTMagic的使用

    // VTMagic的使用 //  CFOrderViewController.m //  qifuyuniOS //// /** *  @author 李洪强, 16-08-30 10:08:50 ...

  4. mysql字段去重方式

    一直找不出某个字段去重的前提下,还能够显示其它字段的数据 以下是解决方法: SELECT *, COUNT(DISTINCT( province)) FROM area_info WHERE type ...

  5. 2016/4/2 json:js和jquery中轻量级数据交换格式 例: 窗口弹出 popwindow

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族 ...

  6. high-level operations on files and collections of files

    11.10. shutil — High-level file operations — Python 3.6.5 documentation https://docs.python.org/3/li ...

  7. spring boot自定义properity

    1.spring boot使用application.properties默认了很多配置. 但有时需要自定义配置.若在application.properties添加属性: app.name=fish ...

  8. String的属性和方法

    package com.zzu.java.array; public class TtString { /** * @author 程路超 * @param args */ public static ...

  9. 替换Android自带apk【转】

    本文转载自:http://www.voidcn.com/article/p-gonowdjh-vz.html 安卓自带的app放在/system/app/下,当我们想要替换这些应用时可以参考如下步骤: ...

  10. js页面报错javax.servlet.jsp.PageContext cannot be resolved to a type解决

    构建了一个maven项目但是项目创建好的jsp总会报错javax.servlet.jsp.PageContext cannot be resolved to a type,但是不影响项目运行.但总归难 ...