HUST 1354 Rubiks
背包。注释写详细了。
本想这样写:每个组内各自做背包,然后组间做背包,但是由于这题M=10000,时间复杂度太大。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<functional>
using namespace std; const int maxn = + ;
int n, m, g; vector<int>G[]; int cost[ + ];
int val[ + ];
int y[];
bool flag[ + ];
int dp1[ + ];
int dp2[ + ]; void init()
{
memset(flag, , sizeof flag);
for (int i = ; i <= g; i++) G[i].clear();
} int main()
{
while (~scanf("%d%d", &n, &m)){ for (int i = ; i <= n; i++) scanf("%d", &cost[i]);
for (int i = ; i <= n; i++) scanf("%d", &val[i]);
scanf("%d", &g);
init();
for (int i = ; i <= g; i++)
{
int f;
scanf("%d", &f);
for (int j = ; j <= f; j++)
{
int id;
scanf("%d", &id);
G[i].push_back(id);
flag[id] = ;
}
scanf("%d", &y[i]);
} memset(dp1, , sizeof dp1); //没有组的归为一组
for (int i = ; i <= n; i++)
{
if (flag[i]) continue;
for (int j = m; j >= cost[i]; j--)
dp1[j] = max(dp1[j], dp1[j - cost[i]] + val[i]);
} for (int k = ; k <= g; k++)
{
//此时dp2数组存储了之前的所有组合最优解
for (int i = ; i <= m; i++) dp2[i] = dp1[i]; //接下来假设不买完这个组合内的魔方,这样DP的话事实上也存在买完的组合,但会被之后的dp2更新,所以不存在问题
for (int s = ; s < G[k].size(); s++)
{
for (int j = m; j >= cost[G[k][s]]; j--)
{
dp1[j] = max(dp1[j], dp1[j - cost[G[k][s]]] + val[G[k][s]]);
}
} //假设买完,用dp2更新
int sum_cost = , sum_val = y[k];
for (int s = ; s < G[k].size(); s++)
{
sum_cost = sum_cost + cost[G[k][s]];
sum_val = sum_val + val[G[k][s]];
} for (int j = m; j >= sum_cost; j--)
{
dp2[j] = max(dp2[j], dp2[j - sum_cost] + sum_val);
} //然后dp1和dp2存下最大值
for (int i = ; i <= m; i++) dp1[i] = max(dp1[i], dp2[i]);
} printf("%d\n", dp1[m]);
}
return ;
}
HUST 1354 Rubiks的更多相关文章
- HUST 1354 - Rubiks (DP)
1354 - Rubiks 时间限制:1秒 内存限制:64兆 452 次提交 102 次通过 题目描述 Isun is a genius. Not only he is an expert in al ...
- HUST 1017 - Exact cover (Dancing Links 模板题)
1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0 ...
- ubuntu 16.04 source (HUST and 163)
#HUST deb http://mirrors.hust.edu.cn/ubuntu/ xenial main restricted universe multiverse deb http://m ...
- Dancing Link --- 模板题 HUST 1017 - Exact cover
1017 - Exact cover Problem's Link: http://acm.hust.edu.cn/problem/show/1017 Mean: 给定一个由0-1组成的矩阵,是否 ...
- hust 1010 最短循环节点
题目链接:http://acm.hust.edu.cn/problem/show/1010 KMP失配指针的利用: next数组前缀和后缀最长公共长度,这样len - next[len];就是最短的循 ...
- light oj 1354 - IP Checking
1354 - IP Checking PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB An I ...
- 1354 - IP Checking(水题)
1354 - IP Checking PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB An I ...
- HUST 1017(DLX)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=65998#problem/A 题意:求01矩阵的精确覆盖. DLX学习资料:ht ...
- [ACM] HUST 1017 Exact cover (Dancing Links,DLX模板题)
DESCRIPTION There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is ...
随机推荐
- C++对文件进行加密解密
1. 起因: 需要对游戏资源进行加密 2. 解决方案: 通过网络查询,xxtea是一款轻量级的加密工具,使用简单方便 3. 加密解密 xxtea只有两个函数,加密:xxtea_encrypt 解密:x ...
- POJ 3468<线段树,区间add>
题目连接 //位运算 k<<1 相当于 k*2 k<<1|1 相当于 k*2+1 /* 修改区间内的值,并且维护区间和. 详见代码 */ #include<cstdio& ...
- oracle 常用sql语句
oracle 常用sql语句 1.查看表空间的名称及大小 select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_sizefrom d ...
- xcconfig 文件配置文件 问题
与公司 QA 聊天,已不止一次被吐槽说移动端从开发环境转到生产环境时,还要靠修改代码来配置对应的环境参数.她认为,从 App 转测试之后,就不应该再修改代码,可以把所有的环境配置都整合到配置文件中,这 ...
- HDU-1232--畅通工程(最小生成树)
Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通 ...
- 使用过滤器(Filter)解决请求参数中文乱码问题(复杂方式)
前述: 在写这篇笔记之前,对笔记中的设计模式进行介绍: 本篇笔记中将要使用到的设计模式是:装饰(包装)设计模式 (1)装饰(包装)设计模式口诀: ...
- java导出和读取excel数据
使用的是poi的jar包 下载地址http://poi.apache.org/download.html 主要是把jar包导入,直接新建一个列子测试一下就明白了.使用起来还是比较方便的,代码里面的原理 ...
- bash报错./mq.sh: line 15: warning: here-document at line 10 delimited by end-of-file (wanted `eof')
[root@localhost tmp]# ./mq.sh./mq.sh: line 15: warning: here-document at line 10 delimited by end-of ...
- laravel 报错 mcrypt_decrypt(): Key of size 11 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported
修改app/config/app.php文件 将key设置成长度为16,24,32的字符串
- CF 313 DIV2 B 树状数组
http://codeforces.com/contest/313/problem/B 题目大意 给一个区间,问你这个区间里面有几个连续相同的字符. 思路: 表示个人用树状数组来写的...了解了树状数 ...