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. Android学习笔记三:用Intent串联activity

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7513399.html 一:Intent Intent可以理解为 意图. 我们可以通过创建intent实例来定义 ...

  2. Python学习摘录(下)

    常用内建模块 1:collections模块:集合模块,提供了许多有用的集合类. namedtuple namedtuple是一个函数,它用来创建一个自定义的tuple对象,并且规定了tuple元素的 ...

  3. iWatch应用开发-oc篇

    1.创建项目 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/di ...

  4. CPU profiling

    http://gernotklingler.com/blog/gprof-valgrind-gperftools-evaluation-tools-application-level-cpu-prof ...

  5. V-rep学习笔记:视觉传感器1

    Vision sensors, which can detect renderable entities(Renderable objects are objects that can be seen ...

  6. Effective前端(3)用CSS画一个三角形

    来源:https://zhuanlan.zhihu.com/p/26160325 三角形的场景很常见,打开一个页面可以看到各种各样的三角形: 由于div一般是四边形,要画个三角形并不是那么直观.你可以 ...

  7. Windows开发进阶之VC++中如何实现对话框的界面重绘

    技术:Windows 系统+Visual studio 2008   概述 应用程序界面是用户与应用程序之间的交互的桥梁和媒介,用户界面是应用程序中最重要的组成部分,也是最为直观的视觉体现.对用户而言 ...

  8. 持续集成工具jenkins的使用

    jenkins类似于Hadson,是一款持续集成工具.使用jenkins完成自动化部署的表现为:当开发人员向版本库提交新的代码后,应用服务器上自动部署,用户或测试人员使用的马上就是最新的应用程序.搭建 ...

  9. socket.io简介

    websocket是一种比较简单的协议,各种语言中都有很多实现版本,实际上它们差别不大,都是在websocket的基础上做些封装,随便选一个即可. socket.io就是众多websocket库中的一 ...

  10. JAVA操作mysql

    所需jar包:mysql-connector-java.jar 代码: import java.sql.*; import java.util.ArrayList; import java.util. ...