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 ...
随机推荐
- 跟踪 Ring3 - Ring0 的运行流程
理论知识 SYSENTER 指令是在 Inter Pentium(R) Ⅱ 处理器上作为"高速系统调用"功能的一部分被首次引用的. SYSENTER 指令进行过专门的优化,能够以最 ...
- 连通性问题--Algorithms IN C读书笔记
近期在看<Algorithms IN C>这本书.刚開始看,读的是英文版的.感觉作者的叙述有点不太easy理解.就找了一本中文版的来看,发现还是看英文版的比較好.先看了第一章的大部分,后面 ...
- Aerospike系列:1:安装
1:下载源文件 wget http://www.aerospike.com/artifacts/aerospike-server-community/3.5.9/aerospike-server-co ...
- Java多线程之线程的状态以及线程间协作通信导致的线程状态转换
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6561589.html 一:线程的状态以及变化图 Java中线程中状态可分为五种:New(新建状态),Ru ...
- mysql改变字符串的大小写
INITCAP:转换每个字的第一个字符为大写LOWER:转换所有字符为小写UPPER:转换所有字符为人写 eg: LOWER(phone)
- 10个超棒jQuery表单操作代码片段
jQuery绝对是一个伟大的开源javascript类库,是帮助我们快速和高效开发前端应用的利器.可能大家在日常的开发过程中常常会处理表单相关的javascript,在今天这篇代码片段分享文章中,这里 ...
- FTP下载工具
开源的FTP下载工具,FTP搬运工.... 01.FileZilla_3.21.0_win64 官方地址: https://filezilla-project.org/ 下载地址: http://pa ...
- Redis C客户端Hiredis代码分析
初始化 redisContext - Redis连接的上下文 /* Context for a connection to Redis */ typedef struct redisContext { ...
- Visual Studio 2015新添加宏
这个宏是类似环境变量,比如$(ProjectDir) $(SolutionDir),这样的,我需要新添加一个变量宏,但是VS的GUI上貌似找不到新的变量的设置,但是Qt的VS插件可以设置$(QTDIR ...
- oracle中生成大批量数据的方法-下
方法五:使用PLSQL的数据生成器 首先测试环境建立:dept表 CREATE TABLE dept(deptno NUMBER(6),dname VARCHAR2(20),loc VARCHAR2( ...