码迷,mamicode.com
首页 > 其他好文 > 详细

hackerrank-knapsack

时间:2015-02-15 14:51:46      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/justinzhang/p/4292837.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!