(codeforces) The Great Mixing
2017-04-18 17:05
627 查看
题目: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
![](http://codeforces.com/predownloaded/73/6a/736a142deac172979a605c006bf429f14e9c9d1f.png)
.
Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke with carbon dioxide concentration
![](http://codeforces.com/predownloaded/0a/22/0a22aebecc884f01f23b04a8d8863079608e4e7d.png)
.
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
![](http://codeforces.com/predownloaded/0a/22/0a22aebecc884f01f23b04a8d8863079608e4e7d.png)
.
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
![](http://codeforces.com/predownloaded/0a/22/0a22aebecc884f01f23b04a8d8863079608e4e7d.png)
,
or -1 if it is impossible.
Examples
input
output
input
output
Note
In the first sample case, we can achieve concentration
![](http://codeforces.com/predownloaded/37/dd/37dd09405bac877eb280ed257a49514589acc518.png)
using
one liter of Coke of types
![](http://codeforces.com/predownloaded/6d/7f/6d7fb716c7b81f17521c3550b08695be09366c43.png)
and
![](http://codeforces.com/predownloaded/57/4b/574bc9bce7feb16aa7af49def7512fc30c0e2c94.png)
:
![](http://codeforces.com/predownloaded/2d/30/2d30cbc815c4541af4986c2c4fdf5af45f06b528.png)
.
In the second case, we can achieve concentration
![](http://codeforces.com/predownloaded/51/b9/51b905edaa781e303774e7588b54f8a328c96551.png)
using
two liters of
![](http://codeforces.com/predownloaded/34/1b/341b4ff969345463c29b396f655513ddd72829ac.png)
type
and one liter of
![](http://codeforces.com/predownloaded/cd/6d/cd6d98ecd513879491bac506ea62dcfd6641a295.png)
type:
![](http://codeforces.com/predownloaded/46/73/467392de45148a690dc51496a3984fae72352686.png)
题意:就是先给你两个数分别是n,k然后再输入k个数,问你在这k个数里最少取多少个数使得他们的平均数为n;
分析:
![](http://codeforces.com/predownloaded/31/1e/311eae983294939d910812c58fa24533c41cc53c.png)
![](http://codeforces.com/predownloaded/af/47/af47c229c603683e765fb9dae6361f14e68dd167.png)
![](http://codeforces.com/predownloaded/00/e3/00e345698ce7595817675509c600c5d810a1f5ae.png)
![](http://codeforces.com/predownloaded/b2/67/b26783027eedf6be62e49b0fb3e579b662eaf337.png)
思路:先按上图进行分析,不难得出每个点的贡献值为(si-n)最后贡献值为0即可。求最小,先将数据进行处理,再进行bfs(这样就可以将所有的组合表示出来,当和为0时,跳出)
代码:
#include <bits/stdc++.h>
using namespace std;
bool visit[1001];
int ans[2002];//记录到达下标当前状态下的最小值
int n,k;
int to;
int i;
vector<int>vec;
void bfs()
{
for(i=0;i<2002;i++) ans[i]=-1;//赋初值
queue<int>q;
q.push(0);
ans[0]=0;//初值
while(!q.empty()){
int v=q.front();q.pop();
for(i=0;i<vec.size();i++){
to=v+vec[i];//当前贡献值
if(to==0){
ans[to]=ans[v]+1;
cout<<ans[to];
return ;
}
if(to>0&&to<2002&&ans[to]==-1){//当出现第一次时肯定为最小
ans[to]=ans[v]+1;//相当于保存bfs的层数
q.push(to);//满足条件进行push()
}
}
}
cout<<-1;
}
int main()
{
for(i=0;i<1001;i++)
visit[i]=false;
scanf("%d%d",&n,&k);
while(k--){
int x;scanf("%d",&x);
if(!visit[x]){//保存每个数的贡献值,因为某些数需要重复出现,但是保存一次就够了
vec.push_back(x-n);
visit[x]=true;
}
}
bfs();
}
drunk with Coke, again. This time they have k types of Coke. i-th
type is characterised by its carbon dioxide concentration
![](http://codeforces.com/predownloaded/73/6a/736a142deac172979a605c006bf429f14e9c9d1f.png)
.
Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke with carbon dioxide concentration
![](http://codeforces.com/predownloaded/0a/22/0a22aebecc884f01f23b04a8d8863079608e4e7d.png)
.
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
![](http://codeforces.com/predownloaded/0a/22/0a22aebecc884f01f23b04a8d8863079608e4e7d.png)
.
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
![](http://codeforces.com/predownloaded/0a/22/0a22aebecc884f01f23b04a8d8863079608e4e7d.png)
,
or -1 if it is impossible.
Examples
input
400 4 100 300 450 500
output
2
input
50 2100 25
output
3
Note
In the first sample case, we can achieve concentration
![](http://codeforces.com/predownloaded/37/dd/37dd09405bac877eb280ed257a49514589acc518.png)
using
one liter of Coke of types
![](http://codeforces.com/predownloaded/6d/7f/6d7fb716c7b81f17521c3550b08695be09366c43.png)
and
![](http://codeforces.com/predownloaded/57/4b/574bc9bce7feb16aa7af49def7512fc30c0e2c94.png)
:
![](http://codeforces.com/predownloaded/2d/30/2d30cbc815c4541af4986c2c4fdf5af45f06b528.png)
.
In the second case, we can achieve concentration
![](http://codeforces.com/predownloaded/51/b9/51b905edaa781e303774e7588b54f8a328c96551.png)
using
two liters of
![](http://codeforces.com/predownloaded/34/1b/341b4ff969345463c29b396f655513ddd72829ac.png)
type
and one liter of
![](http://codeforces.com/predownloaded/cd/6d/cd6d98ecd513879491bac506ea62dcfd6641a295.png)
type:
![](http://codeforces.com/predownloaded/46/73/467392de45148a690dc51496a3984fae72352686.png)
题意:就是先给你两个数分别是n,k然后再输入k个数,问你在这k个数里最少取多少个数使得他们的平均数为n;
分析:
![](http://codeforces.com/predownloaded/31/1e/311eae983294939d910812c58fa24533c41cc53c.png)
![](http://codeforces.com/predownloaded/af/47/af47c229c603683e765fb9dae6361f14e68dd167.png)
![](http://codeforces.com/predownloaded/00/e3/00e345698ce7595817675509c600c5d810a1f5ae.png)
![](http://codeforces.com/predownloaded/b2/67/b26783027eedf6be62e49b0fb3e579b662eaf337.png)
思路:先按上图进行分析,不难得出每个点的贡献值为(si-n)最后贡献值为0即可。求最小,先将数据进行处理,再进行bfs(这样就可以将所有的组合表示出来,当和为0时,跳出)
代码:
#include <bits/stdc++.h>
using namespace std;
bool visit[1001];
int ans[2002];//记录到达下标当前状态下的最小值
int n,k;
int to;
int i;
vector<int>vec;
void bfs()
{
for(i=0;i<2002;i++) ans[i]=-1;//赋初值
queue<int>q;
q.push(0);
ans[0]=0;//初值
while(!q.empty()){
int v=q.front();q.pop();
for(i=0;i<vec.size();i++){
to=v+vec[i];//当前贡献值
if(to==0){
ans[to]=ans[v]+1;
cout<<ans[to];
return ;
}
if(to>0&&to<2002&&ans[to]==-1){//当出现第一次时肯定为最小
ans[to]=ans[v]+1;//相当于保存bfs的层数
q.push(to);//满足条件进行push()
}
}
}
cout<<-1;
}
int main()
{
for(i=0;i<1001;i++)
visit[i]=false;
scanf("%d%d",&n,&k);
while(k--){
int x;scanf("%d",&x);
if(!visit[x]){//保存每个数的贡献值,因为某些数需要重复出现,但是保存一次就够了
vec.push_back(x-n);
visit[x]=true;
}
}
bfs();
}
相关文章推荐
- codeforces 788C The Great Mixing (bitset优化dp、bfs)
- Codeforces 788 C. The Great Mixing
- 【Codeforces 789E】 The Great Mixing 【数学推导+bfs】
- BFS (图)——Codeforces 788 C. The Great Mixing
- Codeforces Round #407 (Div. 2) E The Great Mixing(BFS)
- CF 788C(The Great Mixing-背包)
- Codeforces Round #407 (Div. 1) C. The Great Mixing(bfs)
- codeforces:A. The Great Game
- The Great Mixing
- Codeforces #38H: The Great Marathon 题解
- Codeforces 294E Shaass the Great 树形dp(水
- Codeforces Round #407 (Div. 2)-E-The Great Mixing-滚动数组或者dfs
- 【CF331C】【Codeforces 331C】 The Great Julya Calendar
- codeforces 294E Shaass the Great (树形dp,好题)
- [BFS] Codeforces Round #407 (Div. 1) 788C. The Great Mixing
- codeforces #407(div1c div 2e)The Great Mixing
- CF788C:The Great Mixing(背包bitset & bfs)
- CodeForces 294E Shaass the Great 树形DP
- Codeforces 38H The Great Marathon [dp]
- codeforces April Fools Day Contest 2014 A. The Great Game