Codeforces Round #407 (Div. 1) C. The Great Mixing(bfs)
来源:互联网 发布:360手机数据恢复免费版 编辑:程序博客网 时间:2024/05/20 12:22
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种浓度的饮料(ai/L),让你兑出来n/L浓度的饮料,每种饮料只能取整数升,为至少要多少升饮料.
分析:把所有的ai都减去n,问题就转化为了选择最少的数来凑零,bfs就行了,注意中间状态只需要开到ai的最大值就可以了.
#include <bits/stdc++.h>#define N 1005#define INF 2147483647#define mask 1002using namespace std;int k,n,a[N*N],q[N*N];int vis[2*N],f[2*N];int main(){cin.sync_with_stdio(false);cin>>n>>k;for(int i = 1;i <= k;i++) cin>>a[i];sort(a+1,a+1+k);k = unique(a+1,a+1+k) - a - 1;for(int i = 1;i <= k;i++) a[i] -= n;if(a[1]*a[k] > 0){cout<<-1<<endl;return 0;}int s = 1,t = 1;for(int i = 1;i <= k;i++) {q[t++] = a[i];f[a[i]+mask] = 1;vis[a[i]+mask] = true;}while(s != t && !vis[mask]){for(int i = 1;i <= k;i++) if(abs(a[i] + q[s]) <= 1002 && !vis[a[i] + q[s] + mask]) { f[a[i] + q[s] + mask] = f[q[s] + mask] + 1; vis[a[i] + q[s] + mask] = true; q[t++] = a[i] + q[s]; } s++; }cout<<f[mask]<<endl;}
- Codeforces Round #407 (Div. 1) C. The Great Mixing(bfs)
- [BFS] Codeforces Round #407 (Div. 1) 788C. The Great Mixing
- Codeforces Round #407 (Div. 2) E. The Great Mixing [bfs]
- Codeforces Round #407 (Div. 2)-E-The Great Mixing-滚动数组或者dfs
- (codeforces) The Great Mixing
- codeforces 788C The Great Mixing (bitset优化dp、bfs)
- BFS (图)——Codeforces 788 C. The Great Mixing
- codeforces 788C The Great Mixing( BFS / dp+bitset优化 )
- Codeforces 789E The Great Mixing【Bfs+dp】
- (CF 788)C. The Great Mixing <BFS>
- Codeforces 789E The Great Mixing (数推倒公式 + bfs + 剪枝)
- CF 788C(The Great Mixing-背包)
- Codeforces Round #294 (Div.2) E Shaass the Great
- Codeforces Round #153 (Div. 1) C Number Transformation bfs
- CF788C:The Great Mixing(背包bitset & bfs)
- The Great Mixing
- codeforces788C The Great Mixing
- CodeForces-540C//Codeforces Round #301 (Div. 2)C BFS
- MySQL unknown command ‘\’
- Deceit api-ms-win-crt-private-1-1-0.dll 解决方法
- Web Service
- 优化MySchool总结习题
- Springboot 整合 Mybatis 的完整 Web 案例
- Codeforces Round #407 (Div. 1) C. The Great Mixing(bfs)
- 价牌问题(暴力)
- 有关静态方法和普通方法的调用
- 搜索专题训练hdu1043Eight
- hdu1729取石子游戏 sg!!
- SQL编程
- Java学习----day7——(2)包装类之装箱拆箱
- 安卓驱动之----jni与service的关系初探
- 02-线性结构3 Reversing Linked List (25分)