codeforces 788C The Great Mixing (bitset优化dp、bfs)

33 篇文章 1 订阅
15 篇文章 0 订阅

题目原文 http://codeforces.com/problemset/problem/788/C

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 n, k (0 ≤ n ≤ 1000, 1 ≤ 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.

题目大意:

给你K种浓度的Cola,问最少需要多少升,可以配出制定浓度的Cola

解题思路:

  • 第一种做法 建图+BFS
    官方题解就是这种办法,可以先去看看,根据题意只需要满足 (min)=0 就可以得到符合题意的一组解,易得 1000min1000 。所以我们可以建图,以 [1000,1000] 为节点, min 连边(表示加或减)。然后我们只需要从 0 开始搜索,找到最小的一个回到 0 的环即可。


  • 第二种做法 bitset优化dp
    先考虑最朴素的 dp 做法。大概是这样的:dp[i][sum]表示用了K升cola达到了sum浓度,下面有伪代码。但是这样的复杂度显然达不到我们的预存0因为dp数组仅保存0/1,所以可以使用bitset优化,我们可以实000个sum值 现快速处理2000种情况。移右移实现快速处理2000种情况。

for(int i = 1; i<=1000; i++){

    for(int sum = 0; sum<=2000; sum++){

        for(int j = 0; j<=1000; j++){

            if(j浓度的cola出现过)

                dp[i][sum] |= dp[i-1][sum-(j-n)];

        }

    }
}

细节理解

可以证明的是如果可以配置出要求的浓度最多需要1000升即可,可以这样考虑,给定的浓度为 b ,如果有一个比他小的浓度 a 和一个比他大的浓度 c 。那么最多需要(b-a)升 c 、 (c-b)升 a 就可以得到浓度 b 。那么答案最大即为 c - a. 所以肯定少于1000.

AC代码

/*
    @Author: wchhlbt
    @Date:   2017/3/31
*/
#include <bits/stdc++.h>

#define Fori(x) for(int i=0;i<x;i++)
#define Forj(x) for(int j=0;j<x;j++)
#define maxn 1110007
#define inf 0x3f3f3f3f
#define ONES(x) __builtin_popcount(x)
using namespace std;

typedef long long ll ;
const double eps =1e-8;
const int mod = 10007;
const double PI = acos(-1.0);
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

bitset<2017> dp[2];
int a[2017];
int main()
{
    int n,m,x;
    scanf("%d%d",&n,&m);
    for (int i = 0; i < m; ++i){
        scanf("%d",&x), a[x] = true;
    }
    int now = 0;
    dp[now][1000] = 1;
    for(int i = 1; i<=2000; i++){
        now = 1-now;//滚动数组,重复利用空间
        dp[now].reset();
        for(int j = 0; j<=1000; j++)
        {
            if(a[j]){
                dp[now] |= (dp[1-now]<<j)>>n;//左移 j-n 位
            }
            if(dp[now][1000])
                return 0*printf("%d\n",i);
        }
    }
    puts("-1");
    return 0;
}
/*
    @Author: wchhlbt
    @Date:   2017/3/29
*/
#include <bits/stdc++.h>
#define Fori(x) for(int i=0;i<x;i++)
#define Forj(x) for(int j=0;j<x;j++)
#define maxn 1110007
#define inf 0x3f3f3f3f
#define ONES(x) __builtin_popcount(x)
using namespace std;

typedef long long ll ;
const double eps =1e-8;
const int mod = 10007;
const double PI = acos(-1.0);
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

int d[2017];
bool a[2017];
queue<int> q;
int main()
{
    int n,m,x;
    scanf("%d%d",&n,&m);
    for (int i = 0; i < m; ++i){
        scanf("%d",&x), a[x] = true;
    }
    memset(d,-1,sizeof(d));
    q.push(1000);
    d[1000] = 0;
    while(!q.empty()){
        int x = q.front();
        q.pop();
        for(int i = 0; i<=1000; i++)
        {
            if(a[i])
            {
                if(x + i - n == 1000)
                    return 0*printf("%d\n",d[x]+1);
                else if(x + i - n >=0 && x + i -n <=2000 && d[x +i-n]==-1)
                    d[x +i-n] = d[x] + 1 , q.push(x + i - n);
            }
        }
    }
    puts("-1");
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值