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的更多相关文章

  1. HackerRank "The Indian Job"

    A sly knapsack problem in disguise! Thanks to https://github.com/bhajunsingh/programming-challanges/ ...

  2. 日常小测:颜色 && Hackerrank Unique_colors

    题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献 ...

  3. 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 ...

  4. knapsack problem 背包问题 贪婪算法GA

    knapsack problem 背包问题贪婪算法GA 给点n个物品,第j个物品的重量,价值,背包的容量为.应选哪些物品放入包内使物品总价值最大? 规划模型 max s.t. 贪婪算法(GA) 1.按 ...

  5. [UCSD白板题] Fractional Knapsack

    Problem Introduction Given a set of items and total capacity of a knapsack,find the maximal value of ...

  6. HackerRank "Square Subsequences" !!!

    Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the soluti ...

  7. 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 ...

  8. HackerRank "TBS Problem" ~ NPC

    It is marked as a NPC problem. However from the #1 code submission (https://www.hackerrank.com/Charl ...

  9. (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 ...

  10. HackerRank Extra long factorials

    传送门 今天在HackerRank上翻到一道高精度题,于是乎就写了个高精度的模板,说是模板其实就只有乘法而已. Extra long factorials Authored by vatsalchan ...

随机推荐

  1. MySQL排序:SELECT ORDER BY

    SELECT 和ORDER BY结合进行排序: products表例如以下: a 按产品名称列进行排序: b 按多个列进行排序,默认排序顺序为升序: c 指定排序方向,即指定为降序: d 对多个列进行 ...

  2. 算法笔记_201:第三届蓝桥杯软件类决赛真题(Java本科)

    目录 1 数量周期 2 提取子串 3 源码变换 4 古代赌局 5 火柴游戏   前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 数量周期 [结果填空](满分9分) 复杂现象背后的推动力,可能是极其简 ...

  3. 算法笔记_198:历届试题 打印十字图(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 小明为某机构设计了一个十字型的徽标(并非红十字会啊),如下所示: ..$$$$$$$$$$$$$....$...........$..$$ ...

  4. at java.lang.AbstractStringBuilder.toString

    04-18 14:40:12.615 1210-1210/com.tongyan.lanzhou.rv I/dalvikvm: "main" prio=5 tid=1 RUNNAB ...

  5. 分布式锁和Redisson实现

    http://thoreauz.com/2017/08/20/language/java/%E5%9F%BA%E7%A1%80/%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81 ...

  6. Xamarin.Android之SlidingMenu

    一.前言 有位网友在评论中希望能够出个在Xamarin.Android下实现SlidingMenu效果的随笔,刚好昨天在观看官网示例项目的时候也看到这个SlidingMenu,但是最终的效果并不是我们 ...

  7. Knockout学习之表单绑定器(下)

    “hasFocus”绑定 hasFocus绑定器会将DOM元素的焦点状态与视图模型中的属性相关联,当你设置视图模型中关联的属性为true或false后,将能够设置关键的DOM元素是否获得焦点. 比如下 ...

  8. Lotus Domino开发心得(一)

    —- Lotus Domino 是当今办公自动化系统的主流开发平台之一,目前大部分企业和机构都在使用Lotus Domino 开发自己的无纸办公系统.在开发过程中,我积累了一些小技巧,现在公布出来,希 ...

  9. Kibana 日志查询

    1 概述 很多系统的日志都会放在 Kibana 供查询,就是所谓的 ELK.Kibana 除了可以使用界面供的一些 tab 或者 button 去筛选日志,也可以在搜索栏中使用 Lucene 的语法简 ...

  10. Web-URL编码流程图(详细了解乱码原因)

    URL编码流程图 原URL---->GET时浏览器根据HTTP头的Content-Type的charset,POST根据(<meta http-equiv="Content-Ty ...