题目描述
Perket 是一种流行的美食。为了做好 Perket,厨师们必须小心选择配料,以便达到更好的口感。你有N种可支配的配料。对于每一种配料,我们知道它们各自的酸度 SS 和甜度 BB。当我们添加配料时,总的酸度为每一种配料的酸度总乘积;总的甜度为每一种配料的甜度的总和。
众所周知,美食应该口感适中;所以我们希望选取配料,以使得酸度和甜度的绝对差最小。
另外,我们必须添加至少一种配料,因为没有美食是以白水为主要配料的。
输入格式
第一行包括整数 N,表示可支配的配料数。
接下来 N 行,每一行为用空格隔开的两个整数,表示每一种配料的酸度和甜度。
输入数据保证,如果我们添加所有配料,总的酸度和甜度都不会超过 10^910
9
。
输出格式
输出酸度和甜度的最小的绝对差。
输入输出样例
输入 #1
1
3 10
输出 #1
7
输入 #2
4
1 7
2 6
3 8
4 9
输出 #2
1
说明/提示
1≤N≤10。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cstdio>
#include<math.h>
#include<climits>
#include <map>
using namespace std;
int k,n,m,ans = 0x7fffffff;
int s[30],b[30],c[30];
int ss = 1, bb = 0;
void dfs(int x) {
if(x > n) return ;
else {
for(int i = 1; i <= n; i++) {
if(c[i] == 0 ) {
ss*=s[i];
bb+=b[i];
ans = min(ans,abs(ss-bb) );
c[i] = 1;
dfs(x+1);
c[i] = 0;
ss/=s[i];
bb-=b[i];
}
}
}
}
int main(){
ios_base::sync_with_stdio(0); // 让cin变快
cin.tie(0); // 让cin变快
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> s[i] >> b[i];
}
dfs(1);
cout << ans << endl;
return 0;
}
dfs
===================================================
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cstdio>
#include<math.h>
#include<climits>
#include <map>
using namespace std;
int k,n,m,ans = 0x7fffffff;
int s[30],b[30],c[30];
void dfs(int x,int ss, int bb) {
if(x > n) {
if(ss == 1 && bb == 0) return ;
ans = min(ans,abs(ss-bb) );
return ;
}
dfs(x+1,ss*s[x],bb+b[x] );
dfs(x+1,ss,bb);
}
int main(){
ios_base::sync_with_stdio(0); // 让cin变快
cin.tie(0); // 让cin变快
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> s[i] >> b[i];
}
dfs(1,1,0);
cout << ans << endl;
return 0;
}
本文介绍了一种算法,用于解决如何从多种配料中选择,以制作酸度和甜度绝对差最小的Perket美食。该算法通过深度优先搜索遍历所有可能的配料组合,寻找最优解。
590

被折叠的 条评论
为什么被折叠?



