CF788C:The Great Mixing(背包bitset & bfs)

C. The Great Mixing
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Sasha and Kolya decided to get drunk with Coke, again. This time they have k types of Coke. i-th type is characterised by its carbon dioxide concentration . Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke with carbon dioxide concentration . The drink should also be tasty, so the glass can contain only integer number of liters of each Coke type (some types can be not presented in the glass). Also, they want to minimize the total volume of Coke in the glass.

Carbon dioxide concentration is defined as the volume of carbone dioxide in the Coke divided by the total volume of Coke. When you mix two Cokes, the volume of carbon dioxide sums up, and the total volume of Coke sums up as well.

Help them, find the minimal natural number of liters needed to create a glass with carbon dioxide concentration . Assume that the friends have unlimited amount of each Coke type.

Input

The first line contains two integers nk (0 ≤ n ≤ 10001 ≤ k ≤ 106) — carbon dioxide concentration the friends want and the number of Coke types.

The second line contains k integers a1, a2, ..., ak (0 ≤ ai ≤ 1000) — carbon dioxide concentration of each type of Coke. Some Coke types can have same concentration.

Output

Print the minimal natural number of liter needed to prepare a glass with carbon dioxide concentration , or -1 if it is impossible.

Examples
input
400 4
100 300 450 500
output
2
input
50 2
100 25
output
3
Note

In the first sample case, we can achieve concentration  using one liter of Coke of types  and .

In the second case, we can achieve concentration  using two liters of  type and one liter of  type: .


题意:给N钟不同浓度的二氧化碳,每种有无限瓶,要你配置成k浓度的二氧化碳,求需要的最少瓶二氧化碳。

思路:


图片来自抓不住Jerry的Tom,那么我们将所有浓度预先减去k,题目变成抽取最少的二氧化碳使得总和为0,就是背包问题,那么最多需要多少瓶呢?1000瓶,可以这样考虑,给定的浓度为 b ,如果有一个比他小的浓度 a 和一个比他大的浓度 c 。那么最多需要(b-a)升 c 、 (c-b)升 a 就可以得到浓度 b 。那么答案最大即为 c - a. 所以肯定小于1000,加个bitset优化一下就行了。

因为-1000 <= ai-n <= 1000,所以超出的部分可以忽略。

# include <bits/stdc++.h>

using namespace std;
const int maxn = 1e6;
bitset<2003>b[2];
int a[maxn+3];
int main()
{
    int n, k, t=1;
    scanf("%d%d",&k,&n);
    for(int i=0; i<n; ++i)
    {
        scanf("%d",&a[i]);
        a[i] -= k;
    }
    sort(a, a+n);
    n = unique(a, a+n) - a;
    b[0].set(1000);
    for(int i=1; i<=1000; ++i)
    {
        for(int j=0; j<n; ++j)
        {
            if(a[j] < 0)
                b[t] |= b[1-t] << (-a[j]);
            else
                b[t] |= b[1-t] >> a[j];
        }
        if(b[t][1000])
            return 0*printf("%d\n",i);
        t = 1-t;
        b[t].reset();
    }
    puts("-1");
    return 0;
}
# include <bits/stdc++.h>

using namespace std;
const int maxn = 1e6;
queue<int>q;
int a[maxn+3], b[maxn+3];
int main()
{
    int n, k, t=1;
    scanf("%d%d",&k,&n);
    for(int i=0; i<n; ++i)
    {
        scanf("%d",&a[i]);
        a[i] -= k;
    }
    sort(a, a+n);
    n = unique(a, a+n) - a;
    memset(b, -1, sizeof(b));
    b[1000] = 0;
    q.push(1000);
    while(!q.empty())
    {
        int top = q.front();
        q.pop();
        for(int i=0; i<n; ++i)
        {
            if(top+a[i] == 1000)
                return 0*printf("%d\n",b[top]+1);
            else if((top+a[i]>=0 || top+a[i]<=2000)&&b[top+a[i]]==-1)
                b[top+a[i]] = b[top] + 1, q.push(top+a[i]);
        }
    }
    puts("-1");
    return 0;
}

上面是BFS写法。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值