Gravatar
zcx
积分:311
提交:37 / 109

考虑dp,用$f_i$表示$A$集合比$B$集合多$i$个饼干时$A$集合的最大饼干数(然后按状态定义转移),因为$i$可能是负数所以加一个$5e5$:

    #include<bits/stdc++.h>
    #define int long long
    using namespace std;
     
    const int V = 1e6 + 5;
    const int base = 5e5;
    const int N = 55;
     
    int n;
    int f[V],g[V];
     
    signed main()
    {
        freopen("cookie.in","r",stdin);
        freopen("cookie.out","w",stdout);
        ios::sync_with_stdio(0);
        cin.tie(0);
        cin>>n;
        memset(f,-0x3f,sizeof(f));
        memset(g,-0x3f,sizeof(g));
        g[base] = 0;
        for(int i = 1;i <= n;i++){
            int x;cin>>x;
            for(int j = 0;j <= V - 5;j++) if(j + x <= V - 5) f[j + x] = max(f[j + x],g[j] + x);
            for(int j = 0;j <= V - 5;j++) if(j - x >= 0) f[j - x] = max(f[j - x],g[j]);
            for(int j = 0;j <= V - 5;j++) g[j] = f[j];
        }
        
        cout<<f[base]<<'\n';
        
        return 0;
     }




题目4480  分饼干 AAAAAAAAAA      评论
2026-09-12 15:24:27