HJ41 称砝码
摘要
Title: HJ41 称砝码
Tag: set
Memory Limit: 64 MB
Time Limit: 1000 ms
Powered by:NEFU AB-IN
HJ41 称砝码
-
题意
现有n种砝码,重量互不相等,分别为 m1,m2,m3…mn ;
每种砝码对应的数量为 x1,x2,x3…xn 。现在要用这些砝码去称物体的重量(放在同一侧),问能称出多少种不同的重量。 -
思路
求给出的重量,通过排列组合,能求出多少不同的重量
可以采取set去重,一个set保存答案集合(初始放个0),遍历砝码,每次给答案集合的元素都加上砝码重量,set会自动去重
具体操作实现,可以再建一个set,用来保存上一次答案集合的状态,遍历这个set的元素,加上砝码重量,然后塞进答案集合中 -
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using namespace std;
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;
}