Codeforces 788C 想法+bfs
2017-03-30 21:52
399 查看
The Great Mixing
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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
output
input
output
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:
.
题意:给你n k个数 问你能不能k个数任意取 假设取出m个 能不能组成n的m倍 m最小 不存在输出-1
题解:
(s1+s2+...+sm)/(m*1000)=n/1000
s1+s2+...+sm=m*n
(s1-n)+(s2-n)+...+(sm-n)=0
所以我们把ai都减去n
然后从0开始广搜 搜到0即可
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<deque>
using namespace std;
int n,k;
deque<int>vis;
map<int,int>sp,sp1;
int main(){
scanf("%d%d",&n,&k);
int i,j,x;
for(i=1;i<=k;i++){
scanf("%d",&x);
sp[x-n]=1;
}
vis.push_back(0);
while(!vis.empty()){
int f=vis.front();
vis.pop_front();
for(i=-1000;i<=1000;i++){
if(sp[i]&&!sp1[f+i]){
sp1[f+i]=sp1[f]+1;
if(f+i==0){
printf("%d\n",sp1[f+i]);
return 0;
}
if(f+i>=-1000&&f+i<=1000)vis.push_back(f+i);
}
}
}
printf("-1\n");
return 0;
}
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 2100 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:
.
题意:给你n k个数 问你能不能k个数任意取 假设取出m个 能不能组成n的m倍 m最小 不存在输出-1
题解:
(s1+s2+...+sm)/(m*1000)=n/1000
s1+s2+...+sm=m*n
(s1-n)+(s2-n)+...+(sm-n)=0
所以我们把ai都减去n
然后从0开始广搜 搜到0即可
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<deque>
using namespace std;
int n,k;
deque<int>vis;
map<int,int>sp,sp1;
int main(){
scanf("%d%d",&n,&k);
int i,j,x;
for(i=1;i<=k;i++){
scanf("%d",&x);
sp[x-n]=1;
}
vis.push_back(0);
while(!vis.empty()){
int f=vis.front();
vis.pop_front();
for(i=-1000;i<=1000;i++){
if(sp[i]&&!sp1[f+i]){
sp1[f+i]=sp1[f]+1;
if(f+i==0){
printf("%d\n",sp1[f+i]);
return 0;
}
if(f+i>=-1000&&f+i<=1000)vis.push_back(f+i);
}
}
}
printf("-1\n");
return 0;
}
相关文章推荐
- codeforces 788C The Great Mixing (bitset优化dp、bfs)
- Codeforces 598DIgor In the Museum (BFS加离线优化)
- codeforces 793B - Igor and his way to work(bfs)
- codeforces 601A The Two Routes(BFS)
- codeforces 590C:(BFS)
- CodeForces - 540C Ice Cave(BFS)
- CodeForces 305A Strange Addition(想法题)
- CodeForces 475B Strongly Connected City[想法]
- Pretty Song - CodeForces 509 E 想法题
- Codeforces 659F Polycarp and Hay【BFS】
- codeforces 704B - Ant Man [想法题]
- CodeForces 660D Number of Parallelograms [想法题]
- codeforces 584E Anton and Ira [想法题]
- CodeForces 1208 C. Text Editor BFS广搜
- codeforces 793B - Igor and his way to work(bfs)
- Codeforces 242C King‘s Path(BFS+STL)
- Codeforces 204 A (五一训练 A)+想法题
- codeforces 675C Money Transfers(想法题)
- codeforces_677D. Vanya and Treasure(BFS+DP)
- Codeforces-242C-King's Path(bfs)