Codeforces Round #407 (Div. 1) C. 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.
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 4 100 300 450 500
2
50 2 100 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 1002
using 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) B. Weird journey
- Codeforces Round #325 (Div. 2) D bfs
- Codeforces Round #228 (Div. 1) C. Fox and Card GameGAM
- Codeforces Round #446 (Div. 2) C. PrideIDE
- Codeforces Round #242 (Div. 2) C. Magic FormulasORM
- Codeforces Round #373 (Div. 1) C. Sasha and Array 線段樹
- Codeforces Round #361 (Div. 2) B BFS最短路
- C. Lose it!(思維)Codeforces Round #565 (Div. 3)
- Codeforces Round #215 (Div. 2) C. Sereja and AlgorithmGo
- Codeforces Round #243 (Div. 2) C. Sereja and Swaps
- Codeforces Round #646 (Div. 2)【C. Game On Leaves 題解】GAM
- Codeforces Round #251 (Div. 2) C. Devu and Partitioning of the Arraydev
- Codeforces Round 934 (Div. 1)
- Codeforces Round 947 (Div. 1 + Div. 2)
- Codeforces C. Colored Rooks 構造 (Codeforces Round #518 (Div. 2) )
- Codeforces Round 709 (Div. 1)
- Codeforces Round #688 (Div. 2) C. Triangles(思維,數學)
- C. Dominant Piranha(思維) Codeforces Round #677 (Div. 3)NaN
- Codeforces Round 969 (Div. 1) 記錄
- Codeforces Round #406 (Div. 1) C. Till I Collapse(可持久化線段樹)持久化
- C. Mixing Water
- Codeforces Beta Round #6 (Div. 2 Only) C. Alice, Bob and Chocolate 水題
- Codeforces Round 959 sponsored by NEAR (Div. 1 + Div. 2)
- 【Codeforces Round #499 (Div. 1) B】Rocket
- Codeforces Round #371 (Div. 2) C. Sonya and Queries[Map|二進位制]
- Codeforces Beta Round #76 (Div. 1 Only) A. Frames
- Codeforces Round 943 (Div. 3)(C-G1)
- Codeforces Round #207 (Div. 2)C. Knight Tournament(SET也可以搞定)
- Educational Codeforces Round 165 (Rated for Div. 2) C. Minimizing the Sum題解
- Codeforces Round #639 (Div. 2)
- Codeforces Round #541 (Div. 2)
- Codeforces Round #690 (Div. 3)
- Codeforces Round #682 (Div. 2)
- Codeforces Round #678 (Div. 2)
- Codeforces Round #747 (Div. 2)
- Codeforces Round #673 (Div. 2)
- Codeforces Round #672 (Div. 2)
- Codeforces Round #448 (Div. 2) A