hackerrank-knapsack

时间:2022-08-24 23:39:46

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

  1. HackerRank &quot&semi;The Indian Job&quot&semi;

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

  2. 日常小测:颜色 &amp&semi;&amp&semi; Hackerrank Unique&lowbar;colors

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

  3. hdu 1712&comma; multiple-choice knapsack&comma; 分类: hdoj 2015-07-18 13&colon;25 152人阅读 评论&lpar;0&rpar; 收藏

    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. &lbrack;UCSD白板题&rsqb; Fractional Knapsack

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

  6. HackerRank &quot&semi;Square Subsequences&quot&semi; &excl;&excl;&excl;

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

  7. HackerRank &quot&semi;Minimum Penalty Path&quot&semi;

    It is about how to choose btw. BFS and DFS. My init thought was to DFS - TLE\MLE. And its editorial ...

  8. HackerRank &quot&semi;TBS Problem&quot&semi; ~ NPC

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

  9. &lpar;01背包 当容量特别大的时候&rpar; 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. phpcms 中路径问题

    <table width="100%" border="0" cellspacing="0" cellpadding="0& ...

  2. Tencent&colon;&sol;&sol;Message&sol;协议的实现原理

    腾讯官方通过 Tencent://Message/协议可以让QQ用户显示QQ/TM的在线状态发布在互联网上:并且点击 XXX  ,不用加好友也可以聊天 官方链接: http://is.qq.com/w ...

  3. 【C&num;】Color颜色对照表

    Color.AliceBlue 240,248,255 Color.LightSalmon 255,160,122 Color.AntiqueWhite 250,235,215 Color.Light ...

  4. Java Persistence API(转)

    定义 Java Persistence API JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中.[编辑本段]起源 Sun引入新的JPA ORM规范 ...

  5. rqnoj-342-最不听话的机器人-dp

    dp[i][j][k][[l]: 执行第i步,执行到点(j,k),方向为l时,用的最大步数. 状态转移根据step[i]转移. #include<stdio.h> #include< ...

  6. LeetCode——Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  7. Dubbo广播模式下报错:Can&&num;39&semi;t assign requested address解决办法

    原因: 尝试使用Dubbo的multicast模式,发现一运行就报Can't assign requested address的错误,造成这种原因的主要是系统中开启了IPV6协议(比如window7) ...

  8. ejabberd mod&lowbar;echo 解析

    ejabberd mod_echo 解析(金庆的专栏 2016.8)按开发入门的说明,mod_echo是最简单的模块之一.https://docs.ejabberd.im/developer/当然 m ...

  9. linux文件与目录的创建

    在Linux初期的学习中,是我们对基础命令的掌握,首先我们学习文件与目录的创建,分别有一些命令与选项,我们依次来看: 1:在Linux系统中,一切服务皆以文件的形式表现,脚本文件,服务配置文件,记事本 ...

  10. F&period; Graph Without Long Directed Paths Codeforces Round &num;550 &lpar;Div&period; 3&rpar;

    F. Graph Without Long Directed Paths time limit per test 2 seconds memory limit per test 256 megabyt ...