zoukankan      html  css  js  c++  java
  • 【Leetcode】国王挖金矿

    参考该文章

    https://www.cnblogs.com/henuliulei/p/10041737.html

    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    
    int main (int argc, char** argv){
        
        int workerNumber = 10;
        int auNumber = 5;
        int maxVal = 0;
        int auVal[] = {100, 200, 150, 400, 300};
        int auCost[] = {3, 4, 3, 5, 5};
        bool choose[] = {false, false, false, false, false};
        int valueT[auNumber+1][workerNumber+1];
        std::memset(valueT, 0, sizeof(valueT));
        
        // initialize the valueT
        for(int i = 1; i <= auNumber; i++){
            for(int j = 1; j <= workerNumber; j++){
                if (j < auCost[i-1])
                    valueT[i][j] = valueT[i-1][j];
                else
                    valueT[i][j] = max(valueT[i-1][j], valueT[i-1][j-auCost[i-1]]+auVal[i-1]);
            }
        }
        
        for(int i = 0; i <= auNumber; i++){
            for(int j = 0; j <= workerNumber; j++){
                std::cout << valueT[i][j] << " ";
            }
            std::cout << std::endl;
        }
        
        // get the result
        int k = workerNumber;
        for(int i = auNumber; i > 0; i--){
            if(valueT[i][k] > valueT[i-1][k]){
                choose[i-1] = true;
                
                k = k - auCost[i-1];
            }        
        }
        
        for(int i = 0; i < auNumber; i++)
            cout << choose[i] << " ";
        
        return 0;
    }
  • 相关阅读:
    bind函数
    尾置返回类型
    lambda表达式
    C++谓词
    capacity和size
    容器操作可能会使迭代器失效
    特殊的forward_list操作
    向顺序容器添加元素
    swap与assign
    迭代器
  • 原文地址:https://www.cnblogs.com/gdut-gordon/p/11335112.html
Copyright © 2011-2022 走看看