2 条题解

  • 0
    @ 2025-10-8 17:00:18

    题解一

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    struct node {
        int w, s, v;
        friend bool operator<(const node& a, const node& b) {
            return a.w + a.s < b.w + b.s;
        }
    } a[1010];
    int t, n;
    ll f[20010];
    int main() {
        scanf("%d", &n);
        memset(f, -1, sizeof f);
        for (int i = 1; i <= n; ++i)
            scanf("%d%d%d", &a[i].w, &a[i].s, &a[i].v);
        sort(a + 1, a + 1 + n), f[0] = 0;
        for (int i = 1; i <= n; ++i)
            for (int j = a[i].w + a[i].s; j >= a[i].w; --j)
                f[j] = max(f[j], f[j - a[i].w] + a[i].v);
        ll ans = 0;
        for (int i = 0; i <= 20000; ++i) ans = max(ans, f[i]);
        cout << ans << endl;
        return 0;
    }
    

    题解二

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    struct node {
        int w, s, v;
        friend bool operator<(const node& a, const node& b) {
            return a.w + a.s < b.w + b.s;
        }
    } a[1010];
    int t, n;
    ll f[20010];
    int main() {
        scanf("%d", &n);
        memset(f, -1, sizeof f);
        for (int i = 1; i <= n; ++i)
            scanf("%d%d%d", &a[i].w, &a[i].s, &a[i].v);
        sort(a + 1, a + 1 + n), f[0] = 0;
        for (int i = 1; i <= n; ++i)
            for (int j = a[i].s; j >= 0; --j)
                f[j + a[i].w] = max(f[j + a[i].w], f[j] + a[i].v);
        ll ans = 0;
        for (int i = 0; i <= 20000; ++i) ans = max(ans, f[i]);
        cout << ans << endl;
        return 0;
    }
    
    • 0
      @ 2025-10-8 17:00:09
      #include<bits/stdc++.h>
      using namespace std;
      typedef long long ll;
      struct node {
      	int w,s,v;
      	friend bool operator<(const node&a,const node&b) {
      		return a.w+a.s<b.w+b.s;
      	}
      } a[1010];
      int t,n;
      ll f[20010];
      int main () {
          scanf("%d",&n);
      	memset(f,-1,sizeof f);
      	for(int i=1;i<=n;++i)
              scanf("%d%d%d",&a[i].w,&a[i].s,&a[i].v);
      	sort(a+1,a+1+n),f[0]=0;
      	for(int i=1;i<=n;++i)
              for(int j=a[i].w+a[i].s;j>=a[i].w;--j)
                  f[j]=max(f[j],f[j-a[i].w]+a[i].v);
      	ll ans=0;
      	for(int i=0;i<=20000;++i) ans=max(ans,f[i]);
          cout<<ans<<endl;
      	return 0;
      }

      #include<bits/stdc++.h>
      using namespace std;
      typedef long long ll;
      struct node {
      	int w,s,v;
      	friend bool operator<(const node&a,const node&b) {
      		return a.w+a.s<b.w+b.s;
      	}
      } a[1010];
      int t,n;
      ll f[20010];
      int main () {
          scanf("%d",&n);
      	memset(f,-1,sizeof f);
      	for(int i=1;i<=n;++i)
              scanf("%d%d%d",&a[i].w,&a[i].s,&a[i].v);
      	sort(a+1,a+1+n),f[0]=0;
      	for(int i=1;i<=n;++i)
              for(int j=a[i].s;j>=0;--j)
                  f[j+a[i].w]=max(f[j+a[i].w],f[j]+a[i].v);
      	ll ans=0;
      	for(int i=0;i<=20000;++i) ans=max(ans,f[i]);
          cout<<ans<<endl;
      	return 0;
      }
      • 1

      信息

      ID
      2207
      时间
      1000ms
      内存
      1024MiB
      难度
      7
      标签
      递交数
      18
      已通过
      9
      上传者