题解 | #称砝码#

称砝码

https://www.nowcoder.com/practice/f9a4c19050fc477e9e27eb75f3bfd49c

#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_set>
using namespace std;

struct dp{
    int value;
    vector<int> arr;
};

int main(){
    int n;
    cin >> n;
    int temp, num;
    vector<int> weight1Arr, num1Arr, all;
    for(int i = 1; i <= n; i++){
        cin >> temp;
        weight1Arr.push_back(temp);
    }
    for(int i = 1; i <= n; i++){
        cin >> temp;
        num1Arr.push_back(temp);
    }
    num = weight1Arr.size();
    for(int i = 0; i < num;i++){
        for(int j = 0; j < num1Arr.back(); j++){
            all.push_back(weight1Arr.back());
        }
        num1Arr.pop_back();
        weight1Arr.pop_back();
    }
    unordered_set<int> ans;
    ans.insert(0);
    for(int i = 0; i < all.size(); i++){
        unordered_set<int> temp(ans);
        unordered_set<int>::iterator iter;
        for(iter = temp.begin(); iter != temp.end(); iter++){
            ans.insert(*iter + all[i]);
        }
    }
    cout << ans.size();
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务