E. The Values You Can Make 背包,同时DP
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的更多相关文章
- 背包&数位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][ ...
- 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 ...
- CoderForces 687C The Values You Can Make (01背包,DP)
题意:给定 n 个硬币和一个值 k,问你在用一些硬币组成面值为 k的这些硬币还能组成多少种其他面值. 析:如果这样说,由这些硬币能组成多少种不同的面值,那么是不是就很熟悉了,这不就是01背包么,这个题 ...
- 九度OJ 1123:采药 (01背包、DP、DFS)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2705 解决:1311 题目描述: 辰辰是个很有潜能.天资聪颖的孩子,他的梦想是称为世界上最伟大的医师. 为此,他想拜附近最有威望的医师为师 ...
- HDU 2955 Robberies 背包概率DP
A - Robberies Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submi ...
- [CF189A]Cut Ribbon(完全背包,DP)
题目链接:http://codeforces.com/problemset/problem/189/A 题意:给你长为n的绳子,每次只允许切a,b,c三种长度的段,问最多能切多少段.注意每一段都得是a ...
- HDU5800 To My Girlfriend 背包计数dp
分析:首先定义状态dp[i][j][s1][s2]代表前i个物品中,选若干个物品,总价值为j 其中s1个物品时必选,s2物品必不选的方案数 那么转移的时候可以考虑,第i个物品是可选可可不选的 dp[i ...
- hdu 1561 The more, The Better (依赖背包 树形dp)
题目: 链接:点击打开链接 题意: 非常明显的依赖背包. 思路: dp[i][j]表示以i为根结点时攻击j个城堡得到的最大值.(以i为根的子树选择j个点所能达到的最优值) dp[root][j] = ...
- poj 1015 Jury Compromise(背包变形dp)
In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting of ...
随机推荐
- 微信小程序实战之 pay(支付页面)
项目目录: 逻辑层: pay.js // pages/pay/pay.js Page({ /** * 页面的初始数据 */ data: { resultType: "", resu ...
- Android中的动画具体解释系列【2】——飞舞的蝴蝶
这一篇来使用逐帧动画和补间动画来实现一个小样例,首先我们来看看Android中的补间动画. Android中使用Animation代表抽象的动画类,该类包含以下几个子类: AlphaAnimation ...
- 爬取指定网页的源代码显示在GUI中
建立一个GUI图形界面用来用来输入网址和代码显示的区域 #encoding=utf-8 __author__ = 'heng' #创建一个可以抓取输入网址源代码的GUI from urllib2 im ...
- 【翻译自mos文章】oracle db 中的用户账户被锁--查看oracle用户的尝试次数
參考原文: Users Accounts Getting Locked. (Doc ID 791037.1) 事实上这个文章是为oracle 别的软件产品写的,只是涉及到user 锁定问题.那还是跟d ...
- mysql最新版中文参考手册在线浏览
MySQL是最流行的开放源码SQL数据库管理系统,具有快速.可靠和易于使用的特点.同时MySQL也是一种关联数据库管理系统,具有很高的响应速度和灵活性.又因为mysql拥有良好的连通性.速度和安全性, ...
- Visual Studio Code Unit Testing
1.NUnit project.json { "version": "1.0.0-*", "testRunner": "nunit ...
- 基于开源项目的在线网络视频直播项目---pc端的推流
https://github.com/winlinvip/simple-rtmp-server/issues/66 https://github.com/justinmakaila/iOS-Frame ...
- 嵌入式linux 实现mdev SD卡和U盘自己主动挂载和卸载的方法 mdev.conf
首先先參考这些博客做一些了解:http://linux.chinaunix.net/techdoc/install/2009/11/18/1144936.shtml http://www.cnblog ...
- 欧莱雅浅谈OC中方法调用的顺序中的Category
OC特有的分类Category,依赖于类.它可以在不改变原来的类内容的基础上,为类增加一些方法.分类的使用注意: (1)分类只能增加方法,不能增加成员变量: (2)在分类方法的实现中可以访问原来类中的 ...
- POJ 1927 Area in Triangle(计算几何)
Area in Triangle 博客原文地址:http://blog.csdn.net/xuechelingxiao/article/details/40707691 题目大意: 给你一个三角形的三 ...