【HDU5800】To My Girlfriend(DP + 滚动数组)
来源:互联网 发布:哪里帮人做网络推广 编辑:程序博客网 时间:2024/05/16 00:41
记录一个菜逼的成长。。
题目大意:
指n个物品里面子集的权值和为m,并且这个子集里面有i、j, 没有k、l的子集的个数;(i,j,k,l分别不同)
题解:
令dp[i][j][s1][s2]表示前i个物品填了j的体积,有s1个物品选为为必选,s2个物品选为必不选的方案数(0<=s1,s2<=2),则有转移方程dp[i][j][s1][s2] = dp[i - 1][j][s1][s2] + dp[i - 1][j - a[i]][s1 - 1][s2] + dp[i - 1][j][s1][s2 - 1],边界条件为dp[0][0][0][0] = 1,时间复杂度O(NS*3^2)。
#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <algorithm>#include <cstdlib>#include <vector>#include <set>#include <map>#include <queue>#include <list>#include <deque>#include <cctype>#include <bitset>#include <cmath>using namespace std;#define cl(a) memset(a,0,sizeof(a))typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> PII;typedef pair<LL,LL> PLL;const int INF = 0x3f3f3f3f;const int N = 1000 + 10;const int MOD = 1000000000 + 7;int n,s;int a[N];LL dp[2][N][3][3];//第一维只有2 这是用了滚动数组void add(LL &x,LL y){ x += y; x = (x >= MOD ? x - MOD: x);}void solve(){ memset(dp,0,sizeof(dp)); dp[0][0][0][0] = 1; int pos = 0; for( int i = 1; i <= n; i++ ){ pos = !pos; for( int j = s; j >= 0; j-- ){ for( int s1 = 0; s1 < 3; s1++ ){ for( int s2 = 0; s2 < 3; s2++ ){ //不包含当前物品,也不选为必选或必不选; dp[pos][j][s1][s2] = dp[!pos][j][s1][s2]; if(j >= a[i]){ //包含当前物品,不选为必选或必不选; add(dp[pos][j][s1][s2],dp[!pos][j-a[i]][s1][s2]); //包含当前物品,并选为必选; if(s1 > 0)add(dp[pos][j][s1][s2],dp[!pos][j-a[i]][s1-1][s2]); } if(s2 > 0){ //不包含当前物品,并选为必不选; add(dp[pos][j][s1][s2],dp[!pos][j][s1][s2-1]); } } } } } LL ans = 0; for( int i = 1; i <= s; i++ ){ add(ans,dp[pos][i][2][2]); } printf("%lld\n",(ans * 2) % MOD * 2 % MOD);//这里乘4 是因为i,j,k,l的位置可以互换算不同的方案}int main(){ int T;scanf("%d",&T); while(T--){ scanf("%d%d",&n,&s); for( int i = 1; i <= n; i++ ){ scanf("%d",a+i); } solve(); } return 0;}
0 0
- 【HDU5800】To My Girlfriend(DP + 滚动数组)
- hdu5800 To My Girlfriend(DP)
- hdu5800 To My Girlfriend(dp)
- HDU5800-To My Girlfriend
- 2016多校联赛6H (hdu5800) To My Girlfriend
- 2016多校训练Contest6: 1008 To My Girlfriend hdu5800
- HDU5800 To My Girlfrend(计数dp)
- 【HDOJ 5800】To My Girlfriend(DP)
- HDU-5800-To My Girlfriend-DP
- hdu 5800 To My Girlfriend (dp)
- HDU 5800 To My Girlfriend(dp)
- HDU 5800 To My Girlfriend(dp)
- HDU 5800 To My Girlfriend 【DP】
- HDU 5800 To My Girlfriend 【DP】
- HDU 5800 To My Girlfriend(DP)
- hdu5800To My Girlfriend+DP
- To My Girlfriend HDU
- [HDU 5800] To My Girlfriend (计数DP)
- iOS7真机测试上下出现黑边
- 使用java命令重启tomcat
- thinkphp jq 实现列表查看更多功能
- 判断二叉查找树是否是平衡二叉树(代码)
- uda1341原理及驱动
- 【HDU5800】To My Girlfriend(DP + 滚动数组)
- Uber为何必须尽快上市:增长即将放缓
- Android——使用ListView显示信息列表
- 在java中+=是什么意思
- swift的简单介绍!
- 修改Android系统字体大小
- XSS与字符编码的那些事儿 ---科普文
- IOS开发之瀑布流照片墙实现
- C# Socket简单例子(服务器与客户端通信)