大佬们,help,感谢!!!
查看原帖
大佬们,help,感谢!!!
579961
JunBuJian楼主2022/2/10 19:59
#include<iostream>
#include<algorithm>
#include<cmath>

using namespace std;
const int N = 15;
struct Food{
    int s,b,w;
}f[N];
int n;
bool cmp (struct Food a,struct Food c)
{
    return a.w < c.w;
}
bool st[N];
int sum ;
int S;
int flag = 0;
void dfs(int u ,int s,int b,int sum )
{
//我这个思路是记录所有方案的酸甜值
//在记录的过程中取最小值
//u表示加了几种配料
//s 表示酸度,b表示甜度,sum表示差值
    if(u == n+1)
    {
        return ;
    }
    
    for(int i = 1;i <= n;i ++)
    {
        if(!st[i])
        {
            st[i] = true;
            int x = s, y = b,z = sum;
            s *= f[i].s;
            b += f[i].b;
            S = min(sum,abs(s-b));
           //S 来随时取sum的最小值
            dfs(u + 1,s,b,S);
            s = x,b = y; sum = z;
            st[i] = false ;
        }
    }
}
int main()
{
    cin >> n;
    
    for(int i = 1;i <= n;i ++)
    {
        int s,b,w;
        cin >> s >> b;
        w = abs(s - b);
        f[i] = {s,b,w};
    }
    
    sort(f + 1,f + n+1,cmp);//排序
    dfs(1,1,0,999999);
    
    cout << S; 
    return 0;
}
2022/2/10 19:59
加载中...