zoukankan      html  css  js  c++  java
  • Codeforces Round #407 (Div. 2) E The Great Mixing(BFS)

    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的二氧化碳,问最少需要多少不同浓度的二氧化碳?

    思路:假设需要m个不同浓度的二氧化碳则m满足

    初始化x=0,求出下一次x=0时就得到了最后结果。

     1 #include <iostream>
     2 #include<cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <queue>
     6 #include <map>
     7 using namespace std;
     8 queue<int>que;
     9 int a[1000005];
    10 map<int,int>mp;
    11 int main()
    12 {
    13     int n,m;
    14     scanf("%d%d",&n,&m);
    15     for(int i=0; i<m; i++)
    16     {
    17         scanf("%d",&a[i]);
    18         a[i]=a[i]-n;
    19     }
    20     sort(a,a+m);
    21     int cnt=unique(a,a+m)-a;
    22     que.push(0);
    23     while(!que.empty())
    24     {
    25         int x=que.front();
    26         que.pop();
    27         for(int i=0; i<cnt; i++)
    28         {
    29             int y=a[i]+x;
    30             if(abs(y)>=1001) continue;
    31             if(y==0)
    32             {
    33                 printf("%d
    ",mp[x]+1);
    34                 return 0;
    35             }
    36             if(!mp[y])
    37             {
    38                 mp[y]=mp[x]+1;
    39                 que.push(y);
    40             }
    41         }
    42     }
    43     printf("-1
    ");
    44     return 0;
    45 }
    View Code

    知识点:1.STL中unique是去重函数,2.在BFS的过程中使用map数组进行标记。

  • 相关阅读:
    【Java学习系列】第3课--Java 高级教程
    【夯实PHP基础】nginx php-fpm 输出php错误日志
    【夯实Mysql基础】MySQL性能优化的21个最佳实践 和 mysql使用索引
    【架构设计】分布式文件系统 FastDFS的原理和安装使用
    【13】2016.12.13 周二--《小结2016》
    【诗词歌赋】2016.12.15 周四--文言鸿儒《年终的日常》
    【算法】(查找你附近的人) GeoHash核心原理解析及代码实现
    【夯实Nginx基础】Nginx工作原理和优化、漏洞
    【夯实PHP基础】PHP的反射机制
    HTML DOM简易学习笔记
  • 原文地址:https://www.cnblogs.com/wang-ya-wei/p/6686398.html
Copyright © 2011-2022 走看看