[Codeforces 28D] Do not fear,DravDe is kind

探讨了针对特定条件约束的四元组序列(v,c,l,r)中寻找最大和子序列的问题,通过动态规划的方法,利用排序和最佳值记录来高效解决该问题,并附带详细的代码实现。

Brief Intro:

对于四元组(v,c,l,r),求其子序列中v最大的和,并使其满足:

1、Ci+Li+Ri相同

2、L1=0,Rn=0

3、Li=Sigma(C1...Ci-1)

 

Solution:

算是有条件约束的DP吧

 

设dp[k]为选到k且选k的最大值

对于每个条件,我们这样处理:

1、将所有数据按Ci+Li+Ri的和进行分组处理

2、当Li=0时,将其视为起点;当Ri=0时,用dp[i]去更新res

3、此条件可转换为:仅当Lp=Lq+Cq时,dp[p]可由dp[q]转移而来

 

于是我们将数组按SUM排序后,用Best_val[t]记录Lk+Ck=t时最大的dp[k],用Best_id[t]记录Lk+Ck=t时最大的k,这样每次dp[k]由Best_val[Lk]更新,再由Best_id[Lk]更新父亲节点即可

 

#include <bits/stdc++.h>

using namespace std;

inline int read()
{
    char ch;int num,f=0;
    while(!isdigit(ch=getchar())) f|=(ch=='-');
    num=ch-'0';
    while(isdigit(ch=getchar())) num=num*10+ch-'0';
    return f?-num:num;
}

template<class T> inline void putnum(T x)
{
    if(x<0)putchar('-'),x=-x;
    register short a[20]={},sz=0;
    while(x)a[sz++]=x%10,x/=10;
    if(sz==0)putchar('0');
    for(int i=sz-1;i>=0;i--)putchar('0'+a[i]);
    putchar(' ');
}

const int MAXN=1e5+10;
int n;
struct truck
{
    int v,c,l,r,num;
}dat[MAXN];
int pre[MAXN],dp[MAXN],b_val[MAXN],b_id[MAXN],T[MAXN],start=-1,res=0;

bool cmp(truck x,truck y)
{
    if(x.c+x.l+x.r==y.c+y.l+y.r) return x.num<y.num;
    return x.c+x.l+x.r<y.c+y.l+y.r;
}

int main()
{
    n=read();
    for(int i=1;i<=n;i++)
        dat[i].v=read(),dat[i].c=read(),dat[i].l=read(),dat[i].r=read(),dat[i].num=i;
    
    sort(dat+1,dat+n+1,cmp);
    
    for(int i=1,j=1;i<=n;i=j)
    {
        while(j<=n && dat[i].c+dat[i].l+dat[i].r==dat[j].c+dat[j].l+dat[j].r) j++;
        for(int k=i;k<j;k++)
        {
            if(!dat[k].l) dp[k]=dat[k].v,pre[k]=-1;
            else if(T[dat[k].l]==i)
            {
                dp[k]=b_val[dat[k].l]+dat[k].v;
                pre[k]=b_id[dat[k].l];
            }
            
            int t=dat[k].l+dat[k].c;
            if(T[t]!=i || dp[k]>b_val[t])
            {
                b_val[t]=dp[k];T[t]=i;b_id[t]=k;
            }
            
            if(!dat[k].r && dp[k]>res) res=dp[k],start=k;
        }
    }
    
    vector<int> res;
    while(start!=-1) res.push_back(dat[start].num),start=pre[start];
    putnum(res.size());puts("");
    
    if(!res.size()) return 0;
    for(int i=res.size()-1;i>=0;i--) putnum(res[i]);
    return 0;
}

 

Review:

1、 注意子序列是不能改变相对次序的,因此排序时在SUM相同时要按原ID作为关键字排序(保证稳定性)

2、 对vector.size()少进行减法运算,防止unsigned int溢出

3、 对于具有决策单调性的DP,只要记录当前转移态前最优的状态即可

转载于:https://www.cnblogs.com/newera/p/9086579.html

### 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、付费专栏及课程。

余额充值