Balance Scale

题目描述

You, an experimental chemist, have a balance scale and a kit of weights for measuring weights of powder chemicals.

For work efficiency, a single use of the balance scale should be enough for measurement of each amount. You can use any number of weights at a time, placing them either on the balance plate opposite to the chemical or on the same plate with the chemical. For example, if you have two weights of 2 and 9 units, you can measure out not only 2 and 9 units of the chemical, but also 11 units by placing both on the plate opposite to the chemical (Fig. C-1 left), and 7 units by placing one of them on the plate with the chemical (Fig. C-1 right). These are the only amounts that can be measured out efficiently.

Fig. C-1 Measuring 11 and 7 units of chemical

You have at hand a list of amounts of chemicals to measure today. The weight kit already at hand, however, may not be enough to efficiently measure all the amounts in the measurement list. If not, you can purchase one single new weight to supplement the kit, but, as heavier weights are more expensive, you'd like to do with the lightest possible.

Note that, although weights of arbitrary positive masses are in the market, none with negative masses can be found.

输入

The input consists of at most 100 datasets, each in the following format.

n m
a1 a2 ... an
w1 w2 ... wm
The first line of a dataset has n and m, the number of amounts in the measurement list and the number of weights in the weight kit at hand, respectively. They are integers separated by a space satisfying 1 ≤ n ≤ 100 and 1 ≤ m ≤ 10.

The next line has the n amounts in the measurement list, a1 through an, separated by spaces. Each of ai is an integer satisfying 1 ≤ ai ≤ 109, and ai ≠ aj holds for i ≠ j.

The third and final line of a dataset has the list of the masses of the m weights at hand, w1 through wm, separated by spaces. Each of wj is an integer, satisfying 1 ≤ wj ≤ 108. Two or more weights may have the same mass.

The end of the input is indicated by a line containing two zeros.

输出

For each dataset, output a single line containing an integer specified as follows.

If all the amounts in the measurement list can be measured out without any additional weights, 0.
If adding one more weight will make all the amounts in the measurement list measurable, the mass of the lightest among such weights. The weight added may be heavier than 108 units.
If adding one more weight is never enough to measure out all the amounts in the measurement list, -1.

样例输入

4 2
9 2 7 11
2 9
6 2
7 3 6 12 16 9
2 9
5 2
7 3 6 12 17
2 9
7 5
15 21 33 48 51 75 111
36 54 57 93 113
0 0

样例输出

0
5
-1
5

【题意】

给你n个重量需要称重,你只有m个砝码。请问是否能买一个最小的砝码来满足n个重量呢?

如果不用买输出0,如果买一个也不能达到效果,输出-1。不然就输出买的最小砝码。

【题解】

1、爆搜出所有情况出来

2、答案肯定是原有能组合出来的集合进行,进行差值处理,每次都取交集。

具体可以看代码实现。

【队友代码】

 #pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
const int N = 1e5 + ;
int a[],w[];
int n,m;
set <int> M, ans; void dfs(int pos,int s1,int t1,int S,int T) {
if( pos == m+ ){
M.insert( abs(S-T) );
return ;
}
dfs( pos + , s1 | <<pos , t1 ,S + w[pos],T );
dfs( pos + , s1, t1 | <<pos ,S ,T + w[pos]);
dfs( pos + , s1 , t1 , S, T );
} int main()
{
while( scanf("%d%d",&n,&m) , (n+m) ){
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=;i<=m;i++){
scanf("%d",&w[i]);
}
M.clear();
dfs(,,,,);
bool flag = ;
auto M_end = M.end();
int res = ;
ans.clear();
for(int i=;i<=n;i++)
{
if( M.find(a[i]) == M_end )
{
flag = ;
if(ans.size()==){
for(auto x : M){
ans.insert(x+a[i]);
ans.insert(abs(x-a[i]));
}
ans.insert(a[i]);
}
else{
for(auto x : ans){
if((M.find(x+a[i])==M_end) && (M.find(abs(x-a[i]))==M_end) && x!=a[i]) ans.erase(x);
}
if(ans.size() == ){
res = -;
break;
}
}
}
}
if( flag ){
printf("0\n");
}else{
if(res!=-){
for(auto x:ans){
res = x;
break;
}
}
printf("%d\n",res);
} //cout << M.size() << endl;
}
return ;
}

最新文章

  1. input中空格后的数据不显示
  2. [.net 面向对象程序设计进阶] (6) Lamda表达式(二) 表达式树快速入门
  3. Hybrid框架UI重构之路:一、师其长技以自强
  4. Speech两种使用方法
  5. 在spring,mybatis整合配置中走的弯路(1)
  6. The Perfect Stall
  7. Spark RDD API具体解释(一) Map和Reduce
  8. MVC View返回list列表
  9. HashMap Java Doc
  10. Java程序员面试题集(1-50)(转)
  11. 关于lvs+keepalived只加入一台realserver问题
  12. Nginx CORS实现JS跨域
  13. Android使用百度定位SDK方法及错误处理
  14. 查看linux版本相关命令
  15. 用CAS方案解决高并发一致性问题
  16. 《高性能MySQL(第3版)》【PDF】下载
  17. 【BZOJ3685】【zkw权值线段树】普通van Emde Boas树
  18. LCT模板(BZOJ2631)
  19. java String、StringBuffer、StringBuild、StringTokenizer
  20. 并行开发-Paraller

热门文章

  1. 爬虫界的福利--touchRobot,机器模拟触碰滑动库(已开源)
  2. 2019 DDCTF 部分writeup
  3. sklearn.GridSearchCV选择超参
  4. PHP fopen/file_get_contents与curl性能比较
  5. Razor syntax reference for ASP.NET Core
  6. wmi 远程访问问题解决
  7. 技术干货丨如何在VIPKID中构建MQ服务
  8. VSCode使用Dart和lutter所需按照的插件
  9. 阶段5 3.微服务项目【学成在线】_day18 用户授权_15-细粒度授权-我的课程细粒度授权-实现
  10. jquery图片播放插件Fancybox使用详解