题解 | #称砝码#

称砝码

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

#include <iostream>
#include <string>
#include <stdio.h>
#include <set>
#include <string.h>
#include <algorithm>
#include <map>
#include <iterator>
#include <vector>
#include <ctype.h>
#include <array>
#include <numeric>

using namespace std;

void add_goods_in_vector(vector<int> &goods, int goods_max_num, int weight_per_goods)
{
    int current = 1;
    while (goods_max_num > 0)
    {
        if (goods_max_num >= current)
        {
            goods.push_back(current * weight_per_goods);
            goods_max_num -= current;
            current = current * 2;
        }
        else
        {
            goods.push_back(goods_max_num * weight_per_goods);
            break;
        }
    }
}

int main(void)
{
#ifdef DEBUG_TEST_LYJ
    freopen("input.txt", "r", stdin);
#endif
    int goods_type;
    cin >> goods_type;
    vector<int> goods_weight(goods_type);
    for (int i = 0; i < goods_type; ++i)
    {
        cin >> goods_weight[i];
    }
    vector<int> goods_num(goods_type);
    for (int i = 0; i < goods_type; ++i)
    {
        cin >> goods_num[i];
    }
    int max_weight = 0;
    vector<int> goods_info;
    for (int i = 0; i < goods_type; ++i)
    {
        max_weight += goods_num[i] * goods_weight[i];
        add_goods_in_vector(goods_info, goods_num[i], goods_weight[i]);
    }

    vector<short> dp(max_weight + 1, 0);
    dp[0] = 1;
    for(int index = 0; index < goods_info.size(); ++index) {
        for(int target_weight = max_weight; target_weight >= goods_info[index] ; --target_weight) {
            if(dp[target_weight - goods_info[index]] == 1) {
                dp[target_weight] = 1;
            }
        }
    }

    int sum = accumulate(dp.begin(), dp.end(), 0);
    cout << sum << endl; 

    return 0;
}
全部评论

相关推荐

03-15 14:55
已编辑
门头沟学院 golang
bg:双非学院本&nbsp;ACM银&nbsp;go选手timeline:3.1号开始暑期投递3.7号第二家公司离职顽岩科技&nbsp;ai服务中台方向&nbsp;笔试➕两轮面试,二面挂(钱真的好多😭)厦门纳克希科技&nbsp;搞AI的,一面OC猎豹移动&nbsp;搞AIGC方向&nbsp;一面OC北京七牛云&nbsp;搞AI接口方向&nbsp;一面OC上海古德猫宁&nbsp;搞AIGC方向&nbsp;二面OC上海简文&nbsp;面试撞了直接拒深圳图灵&nbsp;搞AIGC方向一面后无消息懒得问了,面试官当场反馈不错其他小厂没记,通过率80%,小厂杀手😂北京字节&nbsp;具体业务不方便透露也是AIGC后端方向2.28约面&nbsp;(不知道怎么捞的我,我也没在别的地方投过字节简历哇)3.6一面&nbsp;一小时&nbsp;半小时拷打简历(主要是AIGC部分)剩余半小时两个看代码猜结果(经典go问题)➕合并二叉树(秒a,但是造case造了10分钟哈哈)一天后约二面3.12&nbsp;二面,让我挑简历上两个亮点说,主要说的docker容器生命周期管理和raft协议使用二分法优化新任leader上任后与follower同步时间。跟面试官有共鸣,面试官还问我docker底层cpu隔离原理和是否知道虚拟显存。之后一道easy算法,(o1空间解决&nbsp;给定字符串含有{和}是否合法)秒a,之后进阶版如何用10台机加快构建,想五分钟后a出来。面试官以为45分钟面试时间,留了18分钟让我跟他随便聊,后面考了linux&nbsp;top和free的部分数据说什么意思(专业对口了只能说,但是当时没答很好)。因为当时手里有7牛云offer,跟面试官说能否快点面试,马上另外一家时间到了。10分钟后约hr面3.13,上午hr面,下午走完流程offer到手3.14腾讯技术运营约面,想直接拒😂感受:&nbsp;因为有AIGC经验所以特别受AI初创公司青睐,AIGC后端感觉竞争很小(指今年),全是简历拷打,基本没有人问我八股(八股吟唱被打断.jpeg),学的东西比较广的同时也能纵向深挖学习,也运气比较好了哈哈可能出于性格原因,没有走主流Java路线,也没有去主动跟着课写项目,项目都是自己研究和写的哈哈
烤点老白薯:你根本不是典型学院本的那种人,贵了你这能力
查看7道真题和解析
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务