zoj 3812 We Need Medicine (dp 状压)
先贴一下转载的思路和代码,,,:http://blog.csdn.net/qian99/article/details/39138329
状压dp博大精深啊,以后看到n<=50都可以往状压上想,orz
We Need Medicine
Time Limit: 10 Seconds Memory Limit: 65536 KB Special Judge
A terrible disease broke out! The disease was caused by a new type of virus, which will lead to lethal lymphoedema symptom. For convenience, it was named LL virus.
After several weeks of research, the scientists found the LL virus highly lethal and infectious. But more importantly, it has a long incubation period. Many victims were unaware of being infected until everything was too late. To prevent from the apocalypse, we need medicine!
Fortunately, after another several weeks of research, the scientists have finished the analysis of the LL virus. You need write a program to help them to produce the medicine.
The scientists provide you N kinds of chemical substances. For each substance, you can either use it exact Wi milligrams in a medicine, or not use it. Each selected substance will add Ti points of therapeutic effect value (TEV) to the medicine.
The LL virus has Q different variants. For each variant, you need design a medicine whose total weight equals to Mi milligrams and total TEV equals to Si points. Since the LL virus is spreading rapidly, you should start to solve this problem as soon as possible!
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains two integers N (1 <= N <= 400) and Q (1 <= Q <= 400).
For the next N lines, each line contains two integers Wi (1 <= Wi <= 50) and Ti (1 <= Ti <= 200000).
Then followed by Q lines, each line contains two integers Mi (1 <= Mi <= 50) and Si (1 <= Si <= 200000).
Output
For each test case, output Q lines. For the i-th line, output the indexes (1-based) of chemical substances in the i-th medicine, separated by a space. If there are multiple solutions, output any one. If there is no solution, output "No solution!" instead.
Sample Input
1
3 3
2 10
1 12
1 5
3 15
4 27
3 17
Sample Output
1 3
3 2 1
No solution!
Author: JIANG, Kai Source: The 2014 ACM-ICPC Asia Mudanjiang Regional First Round
转自:http://blog.csdn.net/qian99/article/details/39138329
题意:给出n个物品,每个物品有两种属性Wi,Ti,有q组查询,每组查询要求在n个物品中选出一些,并使得两个属性的和为Mi,Si。
思路:刚开始看感觉是神题,后来仔细想了想,其实本质上就是个背包。最裸着写的话,那么就是dp[i][j][k]表示使用前i个物品,是否可以凑出第一个属性j,第二个属性k,要输出方案的话记录一下路径就可以了。一开始这么写了一发,加了一些乱七八糟的优化,还是会T。虽然这题时限还算宽,但这么写复杂度还是太高了。考虑到第一个属性最多只有50,那么可以用一个二进制数来表示是否能凑出第一个属性的情况,即:第i位为1表示可以凑出i。使用这种方法的好处是对于物品i可以直接算出第一种属性的组合情况,枚举一下新增的位,更新一下结果就行了。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 410
#define M 200010
#define mod 6
#define mod2 100000000
#define ll long long
#define ull unsigned long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int T;
int n,q;
int w[N],t[N];
int m,s;
ull f[M];
int ans[M][];
map<ull,int>mt; void ini1()
{
int i;
for(i=;i<=;i++){
mt[ (1ll<<(i-1ll)) ]=i;
}
} void ini()
{
int i,j;
ull k,x;
ull v;
scanf("%d%d",&n,&q);
memset(ans,,sizeof(ans));
memset(f,,sizeof(f));
for(i=;i<=n;i++){
scanf("%d%d",&w[i],&t[i]);
}
f[]=;
for(i=;i<=n;i++){
for(j=;j>=t[i];j--){
v=f[j];
f[j] |= (f[j-t[i]]<<w[i]) & ( (1ll<<)- );
for(k=v ^ f[j];k>;k &= (k-) )
{
x=(k ^(k-))&k;
ans[j][ mt[x]- ]=i;
}
}
}
} void solve()
{
int te;
while(q--)
{
scanf("%d%d",&m,&s);
if(ans[s][m]==){
printf("No solution!\n");
continue;
}
else{
printf("%d",ans[s][m]);
te=ans[s][m];
m-=w[te];
s-=t[te];
while(m!=)
{
printf(" %d",ans[s][m]);
te=ans[s][m];
m-=w[te];
s-=t[te];
}
printf("\n");
}
}
} void out()
{
//printf("%lld\n",ans);
//cout<<ans<<endl;
} int main()
{
ini1();
// freopen("data.in","r",stdin);
scanf("%d",&T);
for(int cnt=;cnt<=T;cnt++)
// while(T--)
//while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF)
{
ini();
solve();
//out();
} return ;
}
再贴一份自己加过注释的,状压dp博大精深啊:
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 410
#define M 200010
#define mod 6
#define mod2 100000000
#define ll long long
#define ull unsigned long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int T;
int n,q;
int w[N],t[N];
int m,s;
ull f[M];
int ans[M][];
map<ull,int>mt; void ini1()
{
int i;
for(i=;i<=;i++){
mt[ (1ll<<(i-1ll)) ]=i;
//printf(" i=%d mt=%I64d\n",i,(1ll<<(i-1ll)));
}
} void ini()
{
int i,j;
ull k,x;
ull v,te;
scanf("%d%d",&n,&q);
memset(ans,,sizeof(ans));
memset(f,,sizeof(f));
for(i=;i<=n;i++){
scanf("%d%d",&w[i],&t[i]);
}
f[]=;
for(i=;i<=n;i++){
//printf(" i=%d\n",i);
//for(j=200000;j>=t[i];j--){
for(j=;j>=t[i];j--){
v=f[j]; //j原来存的值
te=(f[j-t[i]]<<w[i]);
f[j] |= (f[j-t[i]]<<w[i]) ; //j原来存的值+转移后存的值
// printf(" j=%d v=%I64d f=%I64d\n",j,v,f[j]);
for(k=v ^ f[j];k>;k &= (k-) ) //k初始化为新增出来的值,然后不断将k最右边的1减掉
// for(k=te;k>0;k &= (k-1) )
{
x=(k ^(k-))&k; //取k最右边的1
ans[j][ mt[x]- ]=i;
// printf(" k=%I64d x=%I64d mtx-1=%d ans=%d\n",k,x,mt[x]-1,ans[j][ mt[x]-1 ]);
}
}
}
} void solve()
{
int te;
while(q--)
{
scanf("%d%d",&m,&s);
if(ans[s][m]==){
printf("No solution!\n");
continue;
}
else{
printf("%d",ans[s][m]);
te=ans[s][m];
m-=w[te];
s-=t[te];
while(m!=)
{
printf(" %d",ans[s][m]);
te=ans[s][m];
m-=w[te];
s-=t[te];
}
printf("\n");
}
}
} void out()
{
//printf("%lld\n",ans);
//cout<<ans<<endl;
} int main()
{
ini1();
// freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
scanf("%d",&T);
for(int cnt=;cnt<=T;cnt++)
// while(T--)
//while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF)
{
ini();
solve();
//out();
} return ;
}
zoj 3812 We Need Medicine (dp 状压)的更多相关文章
- 【HDU】4352 XHXJ's LIS(数位dp+状压)
题目 传送门:QWQ 分析 数位dp 状压一下现在的$ O(nlogn) $的$ LIS $的二分数组 数据小,所以更新时直接暴力不用二分了. 代码 #include <bits/stdc++. ...
- 【BZOJ】1076 [SCOI2008]奖励关 期望DP+状压DP
[题意]n种宝物,k关游戏,每关游戏给出一种宝物,可捡可不捡.每种宝物有一个价值(有负数).每个宝物有前提宝物列表,必须在前面的关卡取得列表宝物才能捡起这个宝物,求期望收益.k<=100,n&l ...
- CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)
问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...
- HDU5731 Solid Dominoes Tilings 状压dp+状压容斥
题意:给定n,m的矩阵,就是求稳定的骨牌完美覆盖,也就是相邻的两行或者两列都至少有一个骨牌 分析:第一步: 如果是单单求骨牌完美覆盖,请先去学基础的插头dp(其实也是基础的状压dp)骨牌覆盖 hiho ...
- POJ 2404 Jogging Trails [DP 状压 一般图最小权完美匹配]
传送门 题意:找一个经过所有边权值最小的回路,$n \le 15$ 所有点度数为偶则存在欧拉回路,直接输出权值和 否则考虑度数为奇的点,连着奇数条边,奇点之间走已经走过的路移动再走没走过的路 然后大体 ...
- BZOJ 2595: [Wc2008]游览计划 [DP 状压 斯坦纳树 spfa]【学习笔记】
传送门 题意:略 论文 <SPFA算法的优化及应用> http://www.cnblogs.com/lazycal/p/bzoj-2595.html 本题的核心就是求斯坦纳树: Stein ...
- BZOJ 2734: [HNOI2012]集合选数 [DP 状压 转化]
传送门 题意:对于任意一个正整数 n≤100000,如何求出{1, 2,..., n} 的满足若 x 在该子集中,则 2x 和 3x 不能在该子集中的子集的个数(只需输出对 1,000,000,001 ...
- hdu 4352 "XHXJ's LIS"(数位DP+状压DP+LIS)
传送门 参考博文: [1]:http://www.voidcn.com/article/p-ehojgauy-ot.html 题解: 将数字num字符串化: 求[L,R]区间最长上升子序列长度为 K ...
- HDU.4352.XHXJ's LIS(数位DP 状压 LIS)
题目链接 \(Description\) 求\([l,r]\)中有多少个数,满足把这个数的每一位从高位到低位写下来,其LIS长度为\(k\). \(Solution\) 数位DP. 至于怎么求LIS, ...
随机推荐
- leetcode_1049. Last Stone Weight II_[DP]
1049. Last Stone Weight II https://leetcode.com/problems/last-stone-weight-ii/ 题意:从一堆石头里任选两个石头s1,s2, ...
- python简单爬虫爬取百度百科python词条网页
目标分析:目标:百度百科python词条相关词条网页 - 标题和简介 入口页:https://baike.baidu.com/item/Python/407313 URL格式: - 词条页面URL:/ ...
- 动态规划初步--最长上升子序列(LIS)
一.问题 有一个长为n的数列 a0,a1,a2...,an-1a.请求出这个序列中最长的上升子序列的长度和对应的子序列.上升子序列指的是对任意的i < j都满足ai < aj的子序列. 二 ...
- Fiddler模拟POST请求
在进行接口测试时,会模拟post请求,发送不同的请求参数,返回不同的结果,今天我们就来分享一下,怎么用Fiddler工具模拟post请求: 打开Fiddler工具,在右侧点击“composer”的选项 ...
- Mysql,SqlServer,Oracle主键自动增长的设置
在mysql中,如果把表的主键设为auto_increment类型,数据库就会自动为主键赋值.例如: CREATE TABLE google(id INT AUTO_INCREMENT PRIMARY ...
- BZOJ1009: [HNOI2008]GT考试 (矩阵快速幂 + DP)
题意:求一个长度为n的数字字符串 (n <= 1e9) 不出现子串s的方案数 题解:用f i,j表示长度为i匹配到在子串j的答案 用kmp的失配函数预处理一下 然后这个转移每一个都是一样的 所以 ...
- vscode 插件整理
己亥年 庚午月 癸巳日 宜入宅 忌婚嫁 1.Chinese (Simplified) Language Pack for Visual Studio Code 此中文(简体)语言包为 VS Cod ...
- php微信开发自动回复一直提示“该公众号提供的服务出现故障,请稍后再试”
坑:服务器可以接受到发到公众号的信息,但是公众号不能回复,直接echo " ";exit();也会提示“该公众号提供的服务出现故障,请稍后再试”: 可能原因:用的php,是把数组转 ...
- javascript顺序数组简单实现个二分查找
直接上码了注释写得很详细: function bsearch(A,x){ //l:查找范围左 r:查找范围右 let l = 0, //查询范围左边界 r = A.length-1, //查找范围右边 ...
- 【动态规划】bzoj2298: [HAOI2011]problem a
建模超级妙…… Description 一次考试共有n个人参加,第i个人说:“有ai个人分数比我高,bi个人分数比我低.”问最少有几个人没有说真话(可能有相同的分数) Input 第一行一个整数n,接 ...