8.8-甜菜DP测试1-课堂总结

· · 个人记录

8.8-甜菜DP测试1-课堂总结

T4 P17162 [入门赛 #50] 神奇的背包

❌️{\color{red}错误思路:}

❌️{\color{red} 错误代码(}{\color{yellow} 60分}\color{red}):

#include<bits/stdc++.h>
using namespace std;
#define int long long
int a[5005],b[5005],c[5005],dp[5005][5];
signed main(){
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        for(int i=1;i<=n;i++){
            cin>>a[i]>>b[i]>>c[i];
        }
        for(int i=1;i<=n;i++){
            if(c[i]==1){
                for(int j=k;j>=a[i];j--){
                    dp[j][0]=max(dp[j][0],max(dp[j][1],max(dp[j-a[i]][0]+b[i],dp[j-a[i]][1]+b[i])));
                }
            }
            if(c[i]==-1){
                for(int j=a[i];j<=k;j++){
                    dp[j][0]=max(dp[j][0],dp[j-a[i]][0]+b[i]);
                }
            }
        }
        cout<<max(dp[k][1],dp[k][0])<<'\n';
        memset(dp,0,sizeof dp);
    }
    return 0;
}

✅️{\color{green}正确思路:}

✅️{\color{green} 正确代码(}{\color{green} 100分}\color{green}):

#include<bits/stdc++.h>
using namespace std;
#define int long long
struct node{
    int x,y;
};
int dp[50005];
signed main(){
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        vector<node>a,b;
        int n,k;
        cin>>n>>k;
        for(int i=1;i<=n;i++){
            int a1,b1,c;
            cin>>a1>>b1>>c;
            if(c==1)a.push_back({a1,b1});
            else b.push_back({a1,b1});
        }
        for(int i=0;i<a.size();i++){
            for(int j=k;j>=a[i].x;j--){
                dp[j]=max(dp[j],dp[j-a[i].x]+a[i].y);
            }
        }
        int ans=dp[k];
        for(int i=0;i<b.size();i++){
            for(int j=0;j<=k;j++){
                ans=max(ans,dp[j]+(k-j)/b[i].x*b[i].y);
            }
        }
        cout<<ans<<'\n';
        memset(dp,0,sizeof dp);
    }
    return 0;
}

T5 P1586 四方定理

❌️{\color{red}错误思路:}

❌️{\color{red} 错误代码(}{\color{red} 0分}\color{red}):

#include<bits/stdc++.h>
using namespace std;
#define int long long
int dp[65540][10];
signed main(){
    cin.tie(0);
    cout.tie(0);
    for(int i=1;i<=32768;i++){
        for(int j=1;j<=4;j++){
            dp[i][j]+=dp[i-1][j-1];
            if(i>=j)dp[i][j]+=dp[i-j][j];
        }
    }
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        cout<<dp[n*2][4]<<'\n';
    }
    return 0;
}

✅️{\color{green}正确思路:}

✅️{\color{green} 正确代码(}{\color{green} 100分}\color{green}):

#include<bits/stdc++.h>
using namespace std;
#define int long long
int dp[40005][5];
signed main(){
    cin.tie(0);
    cout.tie(0);
    dp[0][0]=1;
    for(int i=1;i<=200;i++){
        for(int j=i*i;j<=32768;j++){
            for(int k=1;k<=4;k++){
                dp[j][k]+=dp[j-i*i][k-1];
            }
        }
    }
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        cout<<dp[n][4]+dp[n][2]+dp[n][3]+dp[n][1]<<'\n';
    }
    return 0;
}

T5 P13957 [ICPC 2023 Nanjing R] 背包

❌️{\color{red}错误思路:}

❌️{\color{red} 错误代码(}{\color{red} 1分}\color{red}):

#include<bits/stdc++.h>
using namespace std;
#define int long long
int dp[32770][10];
signed main(){
    cin.tie(0);
    cout.tie(0);
    cout<<35;
    return 0;
}

✅️{\color{green}正确思路:}

✅️{\color{green} 正确代码}\color{green}:

//没讲,待写