2 条题解

  • 0
    @ 2025-10-8 17:01:01

    by hansang:

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 22;
    typedef long long LL;
    struct node { LL h, w, e; } a[N];
    LL f[(1 << N)], g[(1 << N)];
    int main() {
        int n; LL H; scanf("%d%lld", &n, &H);
        for (int i = 1; i <= n; i++) 
            scanf("%lld%lld%lld", &a[i].h, &a[i].w, &a[i].e);
        memset(f, 0, sizeof(f)); memset(g, 0, sizeof(g));
        for (int i = 1; i <= n; i++) {
            f[1 << (i - 1)] = a[i].e; 
            g[1 << (i - 1)] = a[i].h;
        }
        LL ans = 0; 
        for (int i = 0; i < (1 << n); i++) {
            for (int j = 1; j <= n; j++) if (i & (1 << (j - 1))) {
                int x = i ^ (1 << (j - 1)); LL d = min(a[j].e, f[x] - a[j].w);
                if ((d > f[i]) || (d == f[i] && g[x] + a[j].h > g[i])) {
                    f[i] = d; g[i] = g[x] + a[j].h;
                    if (g[i] >= H) ans = max(ans, f[i]);
                }
            }
        }
        if (ans == 0) printf("Mark is too tall\n");
        else printf("%lld\n", ans);
        return 0;
    }
    
    • 0
      @ 2025-10-8 17:00:47

      by hansang:

      #include<bits/stdc++.h>
      using namespace std;
      const int N=22;
      typedef long long LL;
      struct node{LL h, w, e;} a[N];
      LL f[(1<<N)], g[(1<<N)];
      int main(){
      	int n; LL H; scanf("%d%lld", &n, &H);
      	for(int i=1; i<=n; i++) 
      		scanf("%lld%lld%lld", &a[i].h, &a[i].w, &a[i].e);
      	memset(f, 0, sizeof(f)); memset(g, 0, sizeof(g));
      	for(int i=1; i<=n; i++){
      		f[1<<(i-1)]=a[i].e; 
      		g[1<<(i-1)]=a[i].h;
      	}
      	LL ans=0; 
      	for(int i=0; i<(1<<n); i++){
      		for(int j=1; j<=n; j++) if(i&(1<<(j-1))){
      			int x=i^(1<<(j-1)); LL d=min(a[j].e, f[x]-a[j].w);
      			if((d>f[i]) || (d==f[i] && g[x]+a[j].h>g[i])){
      				f[i]=d; g[i]=g[x]+a[j].h;
      				if(g[i]>=H) ans=max(ans, f[i]);
      			}
      		}
      	}
      	if(ans==0) printf("Mark is too tall\n");
      	else printf("%lld\n", ans);
      	return 0;
      }
      • 1

      USACO(137)动态规划(位向量型)4:奶牛叠罗汉(II)P3112 [USACO14DEC] Guard Mark G

      信息

      ID
      2340
      时间
      1000ms
      内存
      128MiB
      难度
      9
      标签
      递交数
      10
      已通过
      4
      上传者