【搜索+set去重】Balance Scale
Balance Scale
题目描述
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.

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.
输入
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.
输出
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 ;
}
【搜索+set去重】Balance Scale的更多相关文章
- js算法初窥03(简单搜索及去重算法)
前面我们了解了一些常用的排序算法,那么这篇文章我们来看看搜索算法的一些简单实现,我们先来介绍一个我们在实际工作中一定用到过的搜索算法--顺序搜索. 1.顺序搜索 其实顺序搜索十分简单,我们还是以第一篇 ...
- js算法初窥03(搜索及去重算法)
前面我们了解了一些常用的排序算法,那么这篇文章我们来看看搜索算法的一些简单实现,我们先来介绍一个我们在实际工作中一定用到过的搜索算法——顺序搜索. 1.顺序搜索 其实顺序搜索十分简单,我们还是以第一篇 ...
- PostgreSQL 在视频、图片去重,图像搜索业务中的应用
摘要: PostgreSQL 在视频.图片去重,图像搜索业务中的应用作者digoal日期2016-11-26标签PostgreSQL , Haar wavelet , 图像搜索 , 图片去重 , 视频 ...
- 每日英语:Boost Your Balance; Avoid Falls
If you find yourself needing to sit down to take off your shoes, it might be time to start paying at ...
- 48、Spark SQL之与Spark Core整合之每日top3热点搜索词统计案例实战
一.概述 1.需求分析 数据格式: 日期 用户 搜索词 城市 平台 版本 需求: 1.筛选出符合查询条件(城市.平台.版本)的数据 2.统计出每天搜索uv排名前3的搜索词 3.按照每天的top3搜索词 ...
- Robot_bfs
Description The Robot Moving Institute is using a robot in their local store to transport different ...
- Counterfeit Dollar -----判断12枚钱币中的一个假币
Counterfeit Dollar Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u ...
- POJ 1013 Counterfeit Dollar
Counterfeit Dollar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36206 Accepted: 11 ...
- poj1013.Counterfeit Dollar(枚举)
Counterfeit Dollar Time Limit: 1 Sec Memory Limit: 64 MB Submit: 415 Solved: 237 Description Sally ...
随机推荐
- commit 没有提交图片,但是出现了commit的修改
.gitignore里面写上 image/cache/ 就好了
- java 通过runtime 调用python 不显示python运行内容的bug
先说下上面问题的原因,上面问题是因为python中用到了第三方的类库,你的电脑上没有那个类库,所以程序没有运行,在控制台也就看不到输出.只要导入那个类库就好... python 导入类库,可以单独下载 ...
- Jenkins 更新 jenkins.war的方法
Jenkins 有时候更新,直接是主页提示下载 jenkins.war只需要把下载的jenkins.war 替换原来的jenkins.war 就可以了那么问题来了? 原来的 jenkins.war 到 ...
- Mac下不能成功打开uiautomatorviewer的问题解决
在终端运行uiautomatorviewer之后出现下面的错误,检查以后发现环境变量也配置好了 Error: Could not create the Java Virtual Machine. Er ...
- /dev/mem同步写不能使用msync的MS_SYNC选项探究
问题 做了个测试板子的程序,里面有一项写铁电的功能,要求写入之后立即断电,重启后校验数据准确性:铁电设计是通过内存地址直接映射的,于是,使用mmap直接映射了/dev/mem文件,自然地写入之后使用m ...
- docker-compose 布署应用nginx中的create-react-app应用获取环境变量
文章来源:https://www.freecodecamp.org/news/how-to-implement-runtime-environment-variables-with-create-re ...
- MySQL 中视图和表的区别以及联系是什么?
两者的区别: (1)视图是已经编译好的 SQL 语句,是基于 SQL 语句的结果集的可视化的表,而表不是. (2)视图没有实际的物理记录,而基本表有. (3)表是内容,视图是窗口. (4)表占用物理空 ...
- Matlab 图像平移、旋转、缩放、镜像
今天学习了用Matlab实现对图像的基本操作.在Matlab中,图像是按照二维矩阵的形式表示的.所以对图像的操作就是对矩阵的操作. 对图像进行缩放.平移.旋转,都可以转化为矩阵的运算. 关于变换矩阵的 ...
- GIS 空间分析案例分析-错误排除经验总结
GIS 空间分析案例分析-错误排除经验总结 商务科技合作:向日葵,135—4855__4328,xiexiaokui#qq.com 目录:问题发现,问题分析,解决方法,结果 1. problem di ...
- Day5作业,商城+ATM机+后台管理
晚来了....东西太多,需要写的blog内容太多,re讲的渣渣,不明白为什么oldboy经常换老师,吐槽下吧,真心不爱了.... github地址在这:https://github.com/ccorz ...