你可以将已AC的题目连代码加链接发在讨论区,十分感谢!!!

本站为学术网站,非常注重学术诚信!

代码分享仅供学习参考使用(即题解),请勿抄袭题解!!!

代码来源为神秘的惠中傻鱼们。

已更新:二维动规全部习题

正在连载:背包dp

请注意:非题解评论可能被删除

12 条评论

  • @ 2026-4-7 19:59:16
    
    ```cpp
    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
    	long long a[73];
    	long long x;
    	cin >> x;
    	for(int i = 0;i <= 63;i++) a[i] = pow(i,5);
    	//for(int i = 1;i <= 63;i++) cout << a[i] << endl;
        long long b[3];
    	bool y=0;
    	for(int i = 0;i <=63;i++)
    	{
    		for(int j = 0;j <=63;j++)
    		{
    			b[1]=i,b[2]=j;
    			if(a[i]+a[j]==x)
    			{
    				b[1]=i;b[2]=-j;
                    y=1;
    			}
    			if(a[i]-a[j]==x)
    			{
    				b[1]=i;
                    b[2]=j;
    				y=1;
    			}
    			if(y==1)break;
    		}
            if(y==1)break;
    	}
    	cout<<b[1]<<' '<<b[2];
    	return 0;
    }
    
    
    
    
    
    • @ 2026-4-7 18:37:04

      C

      #include<bits/stdc++.h>
      using namespace std;
      int n,m,a,b,ans=0;
      int h[100005];
      bool tp[100005];
      int main(){
      	cin>>n>>m;
      	for(int i=1; i<=100004; i++)tp[i]=1;
      	for(int i=1; i<=n; i++){
      		cin>>h[i];
      	}
      	for(int i=1; i<=m; i++){
      		cin>>a>>b;
      		if(h[b]>=h[a])tp[a]=0;
      		if(h[a]>=h[b])tp[b]=0;
      	}
      	for(int i=1; i<=n; i++)if(tp[i])ans++;
      	cout<<ans;
      	return 0;
      }
      • @ 2026-4-7 18:22:33
        
        ```cpp
        #include<bits/stdc++.h>
        using namespace std;
        bool b[105];
        int main(){
            ios::sync_with_stdio(0);cin.tie(0);
            //freopen(".in","r",stdin);
            //freopen(".out","w",stdout);
            int n,k,d;cin>>n>>k;
            
            for(int j=1;j<=k;j++){
                cin>>d;
                for(int i=1;i<=d;i++){
                    int x;cin>>x;
                    if(b[x]==0)n--;
                    b[x]=1;
                }
            }
            cout<<n;
            return 0;
        }
        
        • @ 2026-4-7 17:57:24

          赛时讨论区↑

          • @ 2026-3-31 18:11:56

            吴奕希 (hzzxwuyixi) @ 12 分钟前
            https://www.gdgzoi.com/problem.php?cid=2830&pid=2

            提示:该代码仅能获得70分,3个点TLE

            #include<bits/stdc++.h>
            
            
            using namespace std;
            const int N=2005,V=505;
            int n,v;
            int m[N],w[N],s[N];
            int dp[2][V];
            int main(){
                cin>>n>>v;
                for(int i=1; i<=n; i++)cin>>m[i]>>w[i]>>s[i];
                for(int i=1; i<=n; i++){
                    for(int j=1; j<=v; j++){
                        for(int k=1; k<=m[i]; k++){
                            if(j>=w[i]*k){
                                dp[1][j]=max(dp[1][j],dp[0][j-w[i]*k]+s[i]*k);
                            }
                        }
                    }
                    for(int j=1; j<=v; j++){
                        dp[0][j]=dp[1][j];
                    }
                }
                cout<<dp[1][v];
                return 0;
            }```
            • @ 2026-3-26 21:29:13

              P448题解 43.138.245.169/d/hongchang/p/P448/solution

              • @ 2026-3-24 20:08:06

                https://www.gdgzoi.com/problem.php?cid=2829&pid=5

                #include<bits/stdc++.h>
                #define ll long long
                using namespace std;
                const int N = 105,mod = 1e6+7;
                int n,be,en,dp[N][N*10],a[N];
                signed main(){
                    ios::sync_with_stdio(false),cin.tie(0);
                    cin >> n >> be >> en;dp[0][be] = 1;
                    for(int i = 1 ; i < n ; i++){
                        cin >> a[i];
                        for(int j = 0 ; j <= en ; j++){
                            if(dp[i-1][j]){
                                int l = j-a[i],r = j+a[i];
                                if(l >= 0) dp[i][l] = 1;
                                if(r <= en) dp[i][r] = 1; 
                            }
                        }
                    }
                     
                    for(int i = en ; i >= 0 ; i--) if(dp[n-1][i]) {cout << i;return 0;}
                    cout << "-1";return 0;
                    return 0;
                }
                
                • @ 2026-3-24 20:05:56

                  https://www.gdgzoi.com/problem.php?cid=2829&pid=4

                  #include<bits/stdc++.h>
                  #define ll long long
                  using namespace std;
                  const int N = 105,mod = 1e6+7;
                  int n,m,dp[N][N],a[N];
                  signed main(){
                      ios::sync_with_stdio(false),cin.tie(0);
                      cin >> n >> m;
                      for(int i = 1 ; i <= n ; i++) cin >> a[i];
                      dp[0][0] = 1;
                      for(int i = 1 ; i <= n ; i++){
                          for(int j = 0 ; j <= m ; j++){
                              if(dp[i-1][j] == 0 ) continue;
                              for(int k = 0 ; k <= a[i]&&k+j<=m ; k++){
                                  (dp[i][j+k] += dp[i-1][j])%=mod;
                              }
                          }
                      }
                      cout << dp[n][m];
                      return 0;
                  }
                  
                  • @ 2026-3-24 20:04:55

                    https://www.gdgzoi.com/problem.php?cid=2829&pid=3

                    #include<bits/stdc++.h>
                    #define ll long long
                    using namespace std;
                    const int N = 105,inf = 0x3f3f3f3f;
                    int n,k,dp[N][N],a[N][N],put[N][N],maxx = -1;
                    signed main(){
                        ios::sync_with_stdio(false),cin.tie(0);
                        cin >> n >> k;
                        for(int i = 1 ; i <= n ; i++){
                            for(int j = 1 ; j <= k ; j++){
                                cin >> a[i][j];
                            }
                        }
                        memset(dp,-0x3f,sizeof dp);
                        dp[0][0] = 0;
                        for(int i = 1 ; i <= n ; i++){
                            for(int p = i ; p <= k-n+i ; p++){
                                for(int j = i-1 ; j < p ; j++){
                                    int pos = dp[i-1][j]+a[i][p];
                                    if(pos > dp[i][p]){
                                        put[i][p] = j;
                                        dp[i][p] = pos;
                                    }
                                }
                            }
                        }
                        int last = 0;
                        for(int i = n ; i <= k ; i++){
                            if(maxx < dp[n][i]){
                                last = i;
                                maxx = dp[n][i];
                            }
                        }
                        vector<int>pos(n+1);
                        for(int i = n ; i >= 1 ; i--){
                            pos[i] = last;
                            last = put[i][last];
                        }
                        cout << maxx << "\n";
                        for(int i = 1 ; i <= n ; i++) cout << pos[i] << " ";
                        return 0;
                    }
                    
                    
                    • @ 2026-3-24 19:47:52

                      erweidonggui

                      https://www.gdgzoi.com/problem.php?cid=2829&pid=2

                      #include <bits/stdc++.h>
                      using namespace std;
                      int f[2005][2005];
                      int main()
                      {
                          string a,b;
                          cin >>a>>b;
                            
                          int n=a.size();
                          int m=b.size();
                          for (int i=1;i<=n;i++)
                          {
                              for (int j=1;j<=m;j++)
                              {
                                  if (a[i-1]==b[j-1])
                                      f[i][j]=f[i-1][j-1]+1;
                                  else
                                      f[i][j]=max(f[i-1][j],f[i][j-1]);
                              }
                          }
                          cout <<f[n][m];
                      }
                      
                      
                      • @ 2026-3-24 19:47:13

                        https://www.gdgzoi.com/problem.php?cid=2829&pid=1

                        #include <bits/stdc++.h>
                        using namespace std;
                          
                        int main() {
                            int n;
                            cin >> n;
                              
                            int t[105][105];
                              
                          
                            for (int i = 1; i <= n; i++) {
                                for (int j = 1; j <= i; j++) {
                                    cin >> t[i][j];
                                }
                            }
                              
                          
                            for (int i = n - 1; i >= 1; i--) {
                                for (int j = 1; j <= i; j++) {
                                    t[i][j] += max(t[i + 1][j], t[i + 1][j + 1]);
                                }
                            }
                            cout << t[1][1] << endl;
                              
                            return 0;
                        }
                          
                        
                        
                        • @ 2026-3-24 19:41:11

                          https://www.gdgzoi.com/problem.php?cid=2829&pid=0

                          #include <bits/stdc++.h>
                          
                          
                          using namespace std;
                          int f[5009][5009];
                          int main()
                          {
                              int n,m;
                              cin>>n>>m;
                              for(int i=0;i<=n;i++)
                              {
                                  f[0][i]=1;
                              }
                              for(int i=0;i<=m;i++)
                              {
                                  f[i][0]=1;
                              }
                              for(int i=1;i<=n;i++)
                              {
                                  for(int j=1;j<=m;j++)
                                  {
                                      f[j][i]=(f[j-1][i]+f[j][i-1])%100000;
                                      //cout<<f[j][i]<<" ";
                                  }
                                  //cout<<endl;
                              }
                              cout<<f[m][n]%10000;
                          }```
                          • 1