hackerrank-knapsack
https://www.hackerrank.com/challenges/unbounded-knapsack 题目描述:
#include <iostream>
#include <vector>
using namespace std; /*
desc:complete knapsack problem, each items can select zero or more times.
auther: justinzhang(aktiger87@gmail.com)
time:2015年2月15日10:14:02
testcase#7:
input:
4
1 2000
1
2 10
5 9
2 2000
2 1
2 2000
1999 1999
=======
output:
2000
10
2000
1999 =====
input:
3
1 6
5
6 8
3 3 3 3 3 3
9 10
9 4 4 9 4 9 9 9 9 output:
5
6
9 */ #define MAXLEN 5000 int main() { int T = 0; // The number of test cases; int n = 0, k = 0; // n is the number of integers, k is the volume of packages. int **array =new int* [MAXLEN];
for(int i = 0; i< MAXLEN; i++) {
array[i] = new int[MAXLEN];
} int tmp = 0; cin>>T; for(int i = 0; i < T; i++) {
vector<int> arrayN; // arrayN should be in this for loop, if it is a global one, you should clear it each time. cin >> n; // the number of integers.
cin >> k; // the volume of package.
for(int j=0; j<n; j++) { cin >> tmp;
arrayN.push_back(tmp); } // here we need to construct the logic of select zero or more times for each item.
for(int j = 0; j < n; j++) {
int maxSelectNum = k/arrayN[j]; for(int select = 0; select < maxSelectNum; select++) { int item = arrayN[j];
arrayN.push_back(item); } } // end of int j=0 , here we finish transition of complete knapsack problem to zeroone knapsack problem // cout << "size of arrayN is :" << arrayN.size() << endl; for(int i=0; i<arrayN.size(); i++) {
// cout << "arrayN:" << arrayN[i] << endl;
}
//cout << "end output arrayN" << endl; for(int vecIndex = 0; vecIndex <=arrayN.size(); vecIndex++) {
for(int volIndex = 0; volIndex <=k ; volIndex++) { array[vecIndex][volIndex] = 0; }
} for(int vecIndex = 1; vecIndex <= arrayN.size(); vecIndex++) { for(int volIndex = 1; volIndex <= k; volIndex++) {
int v1 = 0;
int v2 = 0; v1 = array[vecIndex-1][volIndex]; if(volIndex - arrayN[vecIndex] >= 0 ) { // here we use >=0 , not >0, >0 will be an error, =0 filled the package. v2 = array[vecIndex-1][volIndex-arrayN[vecIndex]] + arrayN[vecIndex]; } if(v1 > v2 ) { array[vecIndex][volIndex] = v1; } else { array[vecIndex][volIndex] = v2; } } // end of for loop volIndex
} // end of for loop int vecIndex cout << array[arrayN.size()][k] << endl;
} // end of loop for int T //释放动态申请的内存
for(int i =0; i < MAXLEN; i++) { delete [] array[i];
} delete[]array; return 0;
}
hackerrank-knapsack的更多相关文章
- HackerRank "The Indian Job"
A sly knapsack problem in disguise! Thanks to https://github.com/bhajunsingh/programming-challanges/ ...
- 日常小测:颜色 && Hackerrank Unique_colors
题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献 ...
- hdu 1712, multiple-choice knapsack, 分类: hdoj 2015-07-18 13:25 152人阅读 评论(0) 收藏
reference: 6.4 knapsack in Algorithms(算法概论), Sanjoy Dasgupta University of California, San Diego Chr ...
- knapsack problem 背包问题 贪婪算法GA
knapsack problem 背包问题贪婪算法GA 给点n个物品,第j个物品的重量,价值,背包的容量为.应选哪些物品放入包内使物品总价值最大? 规划模型 max s.t. 贪婪算法(GA) 1.按 ...
- [UCSD白板题] Fractional Knapsack
Problem Introduction Given a set of items and total capacity of a knapsack,find the maximal value of ...
- HackerRank "Square Subsequences" !!!
Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the soluti ...
- HackerRank "Minimum Penalty Path"
It is about how to choose btw. BFS and DFS. My init thought was to DFS - TLE\MLE. And its editorial ...
- HackerRank "TBS Problem" ~ NPC
It is marked as a NPC problem. However from the #1 code submission (https://www.hackerrank.com/Charl ...
- (01背包 当容量特别大的时候) Knapsack problem (fzu 2214)
http://acm.fzu.edu.cn/problem.php?pid=2214 Problem Description Given a set of n items, each with a ...
- HackerRank Extra long factorials
传送门 今天在HackerRank上翻到一道高精度题,于是乎就写了个高精度的模板,说是模板其实就只有乘法而已. Extra long factorials Authored by vatsalchan ...
随机推荐
- js 判断是否是空对象
主要思路 我们要考虑到的主要有:js原生对象,宿主对象(浏览器上面的). 首先对于宿主对象 主要判断是DOM 对象 和是否是window 对象 是否是DOM对象 value.nodeType 是否存 ...
- js 内置函数 内置对象
1.内置函数 Object Array Boolean Number String Function Date RegExp Error 2.内置对象 Date JSON
- 【麦子学院】Linux cmd命令大全
pwd :print working directory. 打印工作文件夹即当前文件夹. cd :change directory.切换文件夹. /是linux的根文件夹.eg. cd/home ls ...
- qtp descriptive programming multiple language(多语言支持)
so easy, 1,use the descriptive programming; 2,use the | chracter to seperate the different language ...
- 【收藏】常用SQL语句
.1参考手册 ), owner ), species ), sex ), birth DATE, death DATE); //创建表 mysql> show tables; //查看数据库中的 ...
- DLib压缩解压程序示例
/* 这是一个示例程序,使用了Dlib库的compress_stream和cmd_line_parser组件. 这个示例实现了一个简单实用的命令行压缩程序. 当使用-h选项时候,程序输出如下: 使用: ...
- Linux 破坏性修复
1.备份数据 [root@rhel6 ~]# dd count= + records in + records out bytes ( B) copied, 0.000181577 s, 2.8 MB ...
- 修改mysql数据引擎的方法- 提高数据库性能
前言:同学告我说,他为了能使得数据查询变得快一点,修改的数据引擎,故查询一下,总结一下. 登录mysql后,查看当前数据库支持的引擎和默认的数据库引擎,使用下面命令: mysql>show en ...
- 常用bios flash闪存型号
常用flash IC芯片厂商及型号 制造商 4M 8M 16M 32M Atmel AT25DF321AT25DF321A AT25DF641 EON (cFeon) EN25F32EN25P ...
- 基于springboot的多数据源配置
发布时间:2018-12-11 技术:springboot1.5.1 + maven3.0.1+ mybatis-plus-boot-starter2.3.1 + dynamic-datasour ...