codeforces 1442 D Sum 可撤销01背包QwQ

本文介绍了一种针对多个单调不减数组选取元素求最大和的问题,并给出了一种分治优化的方法,将复杂度降至O(nklog),通过枚举不全选的数组并结合01背包动态规划技巧,实现高效求解。

link
题意:
有n个单调不减数组,求从这n个数组中选择k个数的最大和,(只能拿数组的一个前缀)
n,k<3000
思路:因为数组是单调的,所以能得到一个结论就是我们最多只会拿不全一个数组(当有两个数组都拿一部分时,那么肯定可以通过减少某个数组拿的数字去拿另一个数组来增大这个和)。
所以我们就可以通过枚举哪个数组不全选,然后对剩下的数组跑01背包,在枚举当前数组选几个来更新答案,但可惜这是O(nnk)的,
有一个巧妙的优化就是,我们分治整个区间,对于一个l,r维护不选这个区间内的数字的dp值。那么当到达l==r时,就可以直接更新了。O(nklog)
当到达分治的第K层l,r时,我们对k+1层的dp值进行l,mid的更新,然后去递归mid+1,r的区间,然后再回溯的时候在把第k+1层的dp值重新赋值成k层的dp,再对k+1层的dp进行mid+1,r的更新,递归l,mid。
具体实现看代码:

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
const int maxn = 3e3 + 5;


ll dp[20][maxn],c[maxn],a[maxn][maxn];
ll ans;
int n,k;
void init(int d) {
    for(int i=0;i<=k;i++) dp[d][i]=dp[d-1][i];
}
void upd(int d,int l,int r) {
    for(int i=l;i<=r;i++) {
        for(int j=k;j>=c[i];j--) {
            dp[d][j]=max(dp[d][j],dp[d][j-c[i]]+a[i][c[i]]);
        }
    }
}

void sol(int d,int l,int r) {
    if(l==r) {
        ans=max(ans,dp[d][k]);
        for(int i=1;i<=c[l]&&i<=k;i++) {
            ans=max(ans,dp[d][k-i]+a[l][i]);
        }
        return ;
    }
    int mid=l+r>>1;
    init(d+1);
    upd(d+1,l,mid);
    sol(d+1,mid+1,r);

    init(d+1);
    upd(d+1,mid+1,r);
    sol(d+1,l,mid);
}

int main() {
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) {
        scanf("%d",&c[i]);
        for(int j=1;j<=c[i]&&j<=k;j++) {
            scanf("%lld",&a[i][j]);
            a[i][j]+=a[i][j-1];
        }
        for(int j=k+1,d;j<=c[i];j++) scanf("%d",&d);
    }
    sol(1,1,n);
    printf("%lld\n",ans);
    return 0;
}

### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值