https://www.luogu.com.cn/problem/P2036
一、二进制枚举大法
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
const int N = 20;
int n;
int s[N], b[N];
LL MIN = INF;
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> s[i] >> b[i];
//穷举所有可能的酸度和苦度
//使用二进制枚举法,遍历所有可能性,然后分别计算总的酸度和苦度,找出最小的。
int U = 1 << n; //U-1即为全集 ,比如 1<<5 就是 2的5次方,就是32,U=32。而U-1=31,就是表示 1 1 1 1 1
for (int S = 1; S < U; S++) { //枚举所有子集[0,U) //为啥从1开始,因为0代表啥也不选,就是白水
LL sum_s = 1, sum_b = 0;
//是哪些数存在于子集中呢?
for (int i = 0; i < n; i++) {
int bit = (S >> i) & 1;
if (bit) sum_s *= s[i], sum_b += b[i]; //遍历数字S的每一位,如果不是0,表示这一位上的数字是存在的,需要加进来
}
MIN = min(MIN, abs(sum_s - sum_b));
}
cout << MIN << endl;
return 0;
}
2、深度优先搜索
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 15;
int a[N], b[N], n;
int ans = INF;
/**
* @param step 第几步
* @param x 目前的酸度值
* @param y 目前的苦度值
*/
void dfs(int step, int x, int y) {
if (step == n) {
//清水不行~
if (x == 0 && y == 0) return;
//更新ans
ans = min(abs(x - y), ans);
return;
}
//选择
dfs(step + 1, (x == 0 ? 1 : x) * a[step + 1], y + b[step + 1]);
//放弃
dfs(step + 1, x, y);
}
int main() {
//n种配料
cin >> n;
//分别读入酸度和苦度
for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
dfs(0, 0, 0);
//输出
printf("%d
", ans);
return 0;
}