Codeforces 789E The Great Mixing【Bfs+dp】

本文介绍了一种通过混合不同浓度的饮料来达到指定浓度的方法。利用动态规划思想,实现了一个BFS过程,以找到达到目标浓度所需的最少饮料数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

E. 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 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.

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: .


题目大意:

给你K种浓度的药水,让你配出浓度为N的药水、

问最少需要用多少个药水,一种药水可以无限次使用。


思路:


1、要求出浓度为N的药水 ,我们不妨先将每种药水都减去N,那么问题就变成了凑0.


2、那么要怎么凑0呢?

我们设定dp【i】表示浓度为i的药水最少配成需要的药水个数,那么只要设定大小为2000+即可。

然后确定一点,ai<=1000,那么药水的种类最多也就是1000种那么我们此时Bfs维护dp数组的时间复杂度就是O(1000*2000);

可以轻松Ac.


Ac代码:


#include<stdio.h>
#include<string.h>
#include<queue>
#include<map>
using namespace std;
int a[5000];
int dp[5000];
int tot;
void Bfs()
{
    memset(dp,0,sizeof(dp));
    queue<int >s;
    int mid=2000;
    for(int i=0;i<tot;i++)
    {
        dp[a[i]+mid]=1;
        s.push(a[i]);
    }
    while(!s.empty())
    {
        int u=s.front();
        s.pop();
        for(int i=0;i<tot;i++)
        {
            if(u+a[i]>=-1005&&u+a[i]<=1005)
            {
                if(dp[u+a[i]+mid]==0)
                {
                    dp[u+a[i]+mid]=dp[u+mid]+1;
                    s.push(u+a[i]);
                }
            }
        }
    }
    if(dp[0+mid]==0)printf("-1\n");
    else
    printf("%d\n",dp[0+mid]);
}
int main()
{
    int n,k;
    while(~scanf("%d%d",&n,&k))
    {
        map<int ,int >s;
        tot=0;
        for(int i=0;i<k;i++)
        {
            int x;
            scanf("%d",&x);
            x-=n;
            if(s[x]==0)
            {
                s[x]=1;
                a[tot++]=x;
            }
        }
        Bfs();
    }
}











当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值