zoukankan      html  css  js  c++  java
  • 【搜索+set去重】Balance Scale

    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、答案肯定是原有能组合出来的集合进行,进行差值处理,每次都取交集。

    具体可以看代码实现。

    【队友代码】

     1 #pragma GCC optimize(2)
     2 #include<bits/stdc++.h>
     3 using namespace std;
     4 typedef long long ll ;
     5 const int N = 1e5 + 10 ;
     6 int a[110],w[50];
     7 int n,m;
     8 set <int> M, ans;
     9   
    10 void dfs(int pos,int s1,int t1,int S,int T) {
    11     if( pos == m+1 ){
    12         M.insert( abs(S-T) );
    13         return ;
    14     }
    15     dfs( pos + 1 , s1 | 1<<pos , t1 ,S + w[pos],T );
    16     dfs( pos + 1 , s1, t1 | 1<<pos ,S ,T + w[pos]);
    17     dfs( pos + 1 , s1 , t1 , S, T );
    18 }
    19   
    20   
    21 int main()
    22 {
    23     while( scanf("%d%d",&n,&m) , (n+m) ){
    24         for(int i=1;i<=n;i++){
    25             scanf("%d",&a[i]);
    26         }
    27         for(int i=1;i<=m;i++){
    28             scanf("%d",&w[i]);
    29         }
    30         M.clear();
    31         dfs(1,0,0,0,0);
    32         bool flag = 1;
    33         auto M_end = M.end();
    34         int res = 0;
    35         ans.clear();
    36         for(int i=1;i<=n;i++)
    37         {
    38             if( M.find(a[i]) == M_end )
    39             {
    40                 flag = 0;
    41                 if(ans.size()==0){
    42                     for(auto x : M){
    43                         ans.insert(x+a[i]);
    44                         ans.insert(abs(x-a[i]));
    45                     }
    46                     ans.insert(a[i]);
    47                 }
    48                 else{
    49                     for(auto x : ans){
    50                         if((M.find(x+a[i])==M_end) && (M.find(abs(x-a[i]))==M_end) && x!=a[i]) ans.erase(x);
    51                     }
    52                     if(ans.size() == 0){
    53                         res = -1;
    54                         break;
    55                     }
    56                 }
    57             }
    58         }
    59         if( flag ){
    60             printf("0
    ");
    61         }else{
    62             if(res!=-1){
    63                 for(auto x:ans){
    64                     res = x;
    65                     break;
    66                 }
    67             }
    68             printf("%d
    ",res);
    69         }
    70   
    71         //cout << M.size() << endl;
    72     }
    73     return 0 ;
    74 }
    View Code
  • 相关阅读:
    本地邮件系统的安装及配置
    通用性站点管理后台(Bee OPOA Platform) (2) 快速开发特性
    (转).NET Framework 自动内存管理机制深入剖析 (C#分析篇)
    通用性站点管理后台(Bee OPOA Platform) (3) MVC特性
    通用性站点管理后台(Bee OPOA Platform) (4) DAL
    通用性站点管理后台(Bee OPOA Platform) (1)
    【讨论】一个接口的世界
    Ubuntu Mono 初体验
    发现assembly的dll在temp里,AppDomain无法动态加载那个assembly
    熟知android模拟器的快捷键
  • 原文地址:https://www.cnblogs.com/Osea/p/11397666.html
Copyright © 2011-2022 走看看