• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
jacklee404
Never Stop!
博客园    首页    新随笔    联系   管理    订阅  订阅
Problem - 788C-The Great Mixing-(BFS+数值分析)

题目

C. 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 img. Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke with carbon dioxide concentration img. 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 img. 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, ..., a**k (0 ≤ a**i ≤ 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 img, or -1 if it is impossible.

Examples

input

Copy

400 4
100 300 450 500

output

Copy

2

input

Copy

50 2
100 25

output

Copy

3

Note

In the first sample case, we can achieve concentration img using one liter of Coke of types img and img: img.

In the second case, we can achieve concentration img using two liters of img type and one liter of img type: img.

思路

Lemma

​ 对于一个序列\(a_1, a_2, a_3, a_4, ... ,a_n\), 且\(a_i \in [-k, k]\),设\(f(x) = \sum \limits^x_i {a_i}\), 且 \(f(n) = 0\),则将\(a_i\)的顺序进行一定的排列,可以使\(f(x) \in [0, k]\)。

​ 严格的证明真是不会了,这里给出一个直观的证明由介值定理,证明\(f(x) \in [0, k]\),我们只需要证明该区间的最大值是\(x \in [0, k]\), 最小值是0.

​ 因为\(f(n) = 0\), \(max(f(1)) = max(a[i]) \in [0, k]\) , 我们假设\(f(1)\)为k,直观上理解,若使该函数递减至小于0,那么该数不合法,若使该函数大于k,我们可以排列将其值在0,k之间波动。

知道这些后,我们便可以通过BFS使其降低复杂度。

做法

因为上述公式化简后,可以变为

\(\sum \limits^n_{i =1}(a_i - n) = 0\)

所以我们只需要进行BFS枚举每一步该选什么值,然后搜索出最短路,复杂度为\(O((2n)^2)\)

通过上述引理,我们可以将数值缩小在\([0, n]\) 间复杂度\(O(n^2)\)

另外我们并不需要考虑\(k\)的取值,而是考虑\(a_i - n\)的取值

另外这道题也可以当成完全背包来做,每个物品的价值为\(a_i-n\), 那么求的就是考虑前i个物品价值为0时的最小物品容量

Code

#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <cstring>
using namespace std;

const int N = 1e3 + 10;

int a[N], cnt, vis[N], dep[N];

/* 	
		很巧妙的一道题, 最终将
  问题转化为求: 从一个序列中任意挑出k个数,使其和为0,求最小的k
  这个问题再转化为bfs也很巧妙
  另外有个结论 对于 -k k 之间的数若存在一个前缀和序列为0那么调整一下,可以保证
  任意的前缀和0, k之间
*/

int bfs(){
	memset(dep, -1, sizeof dep);
	queue<int> q1;
	q1.push(0), dep[0] = 0;
	while(q1.size()) {
		int t = q1.front(); q1.pop();
		for(int i = 1; i <= cnt; i ++) {
			if(t + a[i] == 0){
				return dep[t] + 1;
			}
 			if(t + a[i] > 0 && t + a[i] <= 1000 && dep[t + a[i]] == -1) {
				q1.push(t + a[i]);
				dep[t + a[i]] = dep[t] + 1;
			}
		}
	}
	return -1;
}	

int main() {
	int n, k;
	cin >> n >> k;
	for(int i = 1; i <= k; i ++) {
		int t;
		cin >> t;
		if (!vis[t]) vis[t] = true, a[++cnt] = t - n;
	}
	cout << bfs();
}
posted on 2023-01-13 13:04  Jack404  阅读(24)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3