Codeforces 789E The Great Mixing【Bfs+dp】
来源:互联网 发布:eplan电气绘图软件 编辑:程序博客网 时间:2024/05/09 17:31
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.
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.
Print the minimal natural number of liter needed to prepare a glass with carbon dioxide concentration, or-1 if it is impossible.
400 4100 300 450 500
2
50 2100 25
3
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 789E The Great Mixing【Bfs+dp】
- Codeforces Round #407 (Div. 2) E. The Great Mixing [bfs]
- Codeforces 789E The Great Mixing (数推倒公式 + bfs + 剪枝)
- (codeforces) The Great Mixing
- codeforces 788C The Great Mixing (bitset优化dp、bfs)
- codeforces 788C The Great Mixing( BFS / dp+bitset优化 )
- Codeforces Round #407 (Div. 1) C. The Great Mixing(bfs)
- BFS (图)——Codeforces 788 C. The Great Mixing
- [BFS] Codeforces Round #407 (Div. 1) 788C. The Great Mixing
- Codeforces Round #407 (Div. 2)-E-The Great Mixing-滚动数组或者dfs
- 【树形DP】 CodeForces 294E Shaass the Great
- codeforces 294E Shaass the Great (树形dp,好题)
- Codeforces 294E Shaass the Great 树形dp(水
- (CF 788)C. The Great Mixing <BFS>
- CF788C:The Great Mixing(背包bitset & bfs)
- The Great Mixing
- codeforces788C The Great Mixing
- CF 788C(The Great Mixing-背包)
- unity ugui对场景中鼠标点击事件的遮挡
- spring包详解
- sql with as 用法
- python学习之argparse模块
- SQL语言的分类
- Codeforces 789E The Great Mixing【Bfs+dp】
- 意甲官网在中国地区被假网劫持,竟声称赞助?
- linux内核地址映射
- 垃圾邮件和不合法邮件预测和过滤
- SLIC图像超像素分割算法解析
- 多线程
- java list(属性方法)
- Mahout的taste里的几种相似度计算方法
- Microsoft Office Visio 2003画ER图