其他分享
首页 > 其他分享> > 洛谷 P2392 kkksc03考前临时抱佛脚, dp / 深搜

洛谷 P2392 kkksc03考前临时抱佛脚, dp / 深搜

作者:互联网

题目链接

P2392 kkksc03考前临时抱佛脚 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

 

题目

 

 

 

dp代码

#include <iostream>
#include <cmath>
#include <cstring>

using namespace std;

int a[4][25], dp[2000];
int s[4];
int sum[4];

int main()
{
    cin >> s[0] >> s[1] >> s[2] >> s[3];
    
    for(int i = 0; i < 4; ++ i )
        for(int j = 0; j < s[i]; ++ j)
            cin >> a[i][j], sum[i] += a[i][j];
    
    int res = 0;
    for(int i = 0; i < 4; ++ i)
    {
        memset(dp, 0, sizeof dp);
        int ans = 0;
        for(int k = 0; k < s[i]; ++ k)
            for(int j = sum[i]/2; j >= a[i][k]; -- j)
            {
                dp[j] = max(dp[j], dp[j-a[i][k]] + a[i][k]);
                ans = max(ans, dp[j]);
            }
            res += sum[i] - ans;
    }
    
    cout << res << endl;
    return 0;
}

 

 

深搜代码

#include <iostream>

using namespace std;

int a[4][25];
int s[4];
bool st[30];
int l = 0, r = 0, res = 0, num = 999999999;

void dfs(int i, int j)
{
    if(j < 0) 
    {
        num = min(num, max(l, r));
        return;
    }
    l += a[i][j];
    dfs(i, j-1);
    l -= a[i][j];
    r += a[i][j];
    dfs(i, j-1);
    r -= a[i][j];
    
}


int main()
{
    cin >> s[0] >> s[1] >> s[2] >> s[3];
    
    for(int i = 0; i < 4; ++ i )
        for(int j = 0; j < s[i]; ++ j)
            cin >> a[i][j];
    
    for(int i = 0; i < 4; ++ i)
    {
        dfs(i, s[i]-1);
        res+= num;
        l = 0, r = 0, num = 999999999;
    }
    
    cout << res << endl;
    return 0;
}

 

标签:洛谷,P2392,int,res,++,num,ans,kkksc03,dp
来源: https://www.cnblogs.com/la-la-wanf/p/14787376.html