HJ41 称砝码

Powered by:NEFU AB-IN

Link

文章目录

HJ41 称砝码

题意

现有n种砝码,重量互不相等,分别为 m1,m2,m3...mn ;

每种砝码对应的数量为 x1,x2,x3...xn 。现在要用这些砝码去称物体的重量(放在同一侧),问能称出多少种不同的重量。

思路

求给出的重量,通过排列组合,能求出多少不同的重量

可以采取set去重,一个set保存答案集合(初始放个0),遍历砝码,每次给答案集合的元素都加上砝码重量,set会自动去重

具体操作实现,可以再建一个set,用来保存上一次答案集合的状态,遍历这个set的元素,加上砝码重量,然后塞进答案集合中
*

代码

cpp 复制代码
#include <bits/stdc++.h>
#include <vector>
using namespace std;
#define int long long
#undef int

#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define IOS                                                                                                            \
    ios::sync_with_stdio(false);                                                                                       \
    cin.tie(nullptr);                                                                                                  \
    cout.tie(nullptr)
#define DEBUG(X) cout << #X << ": " << X << '\n'

const int N = 1e2 + 10, INF = 0x3f3f3f3f;

int a[N];
vector<int> v;
signed main()
{
    // freopen("Tests/input_1.txt", "r", stdin);
    IOS;

    set <int> s;

    int n;
    cin >> n;
    for(int i = 1; i <= n; ++i) cin >> a[i];

    for(int i = 1; i <= n; ++ i){
        int x;
        cin >> x;
        for(int j = 1; j <= x; ++ j) v.push_back(a[i]);
    }

    s.insert(0);
    for(auto i : v){
        set <int> tmp(s);
        for(auto k : tmp) s.insert(k + i);
    }

    cout << SZ(s);

    return 0;
}
相关推荐
Hesionberger9 分钟前
动态规划与二分法破解最长递增子序列
java·数据结构·python·算法·leetcode
imuliuliang22 分钟前
关于Kruskal 算法在图优化问题中的扩展应用7
算法
老约家的可汗1 小时前
Linux中基础IO
linux·服务器·算法
满怀冰雪1 小时前
第29篇-状态压缩DP-当状态很多时如何降维优化
java·算法·动态规划
令狐掌门1 小时前
2026华为OD面试题020:日志文件异常检测
算法·leetcode·华为od
炸薯条!1 小时前
从零开始学C++(4) --类和对象
开发语言·c++·算法
haluhalu.1 小时前
prompts.chat:07-few-shot-prompting
人工智能·算法
Jerry1 小时前
LeetCode 150. 逆波兰表达式求值
算法
浩瀚地学1 小时前
【面试算法笔记】0106-数组-区间和
笔记·算法·面试
Jerry7 小时前
LeetCode 1047. 删除字符串中的所有相邻重复项
算法