Codeforces Round #407 (Div. 2) E. The Great Mixing [bfs]

E. 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 nk (0 ≤ n ≤ 10001 ≤ 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 2
100 25
output
3
题意:给你k种不同浓度的二氧化碳,要求以最少的混合次数求出浓度为n/1000的二氧化碳

题解:一共1000种浓度,所以推论最多1000次混合可以得出答案,大于1000次则不存在。

由于推论最多1000次可以得出答案,所以我用mark[step][value]记录当前step次混合后 浓度等于value的状态是否存在 进行bfs

由于最多1000种浓度 所以每一步最多1000个状态 复杂度为1000*1000

AC代码:

#include<stdio.h>
#include<queue>
#include<map>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
ll f[1005];
ll a[1005],tot=0;
map<ll,ll>mark[1005];
struct node
{
	ll v,step;
}M;
ll bfs(ll ans)
{
	queue<node>que;
	for(ll i=0;i<tot;i++)
	{
		M.v=a[i];
		M.step=1;
		que.push(M);
	}
	while(!que.empty())
	{
		node k=que.front();
		que.pop();
		if(k.step>1000)return -1; //若混合次数大于1000次则不存在 
		for(ll i=0;i<tot;i++)
		{
			if(k.v<ans*k.step&&a[i]<ans)continue;   //若当前浓度<答案浓度 则要加入比答案浓度大的浓度 
			if(k.v>ans*k.step&&a[i]>ans)continue;   //若当前浓度>答案浓度 则要加入比答案浓度小的浓度 
			M.v=(k.v+a[i]);
			M.step=k.step+1;
			if(mark[M.step][M.v]==0)
			{
				mark[M.step][M.v]=1;
				if(M.v==ans*M.step)
					return M.step;
				que.push(M);
			}
		}
	}
	return -1;
}
int main()
{
	ll ans,n;
	scanf("%lld%lld",&ans,&n);
	for(ll i=0;i<n;i++)
	{
		ll k;
		scanf("%lld",&k);
		if(f[k]==0)
		{
			f[k]=1;
			a[tot++]=k;
		}
		if(k==ans)
		{
			printf("1\n");
			return 0;
		} 
	}
	sort(a,a+tot);
	if(a[0]>ans||a[tot-1]<ans)printf("-1\n");  //若最小的浓度>答案浓度 或者 最大的浓度<答案浓度 则不可能达到要求 
	else printf("%lld\n",bfs(ans));
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值