zoukankan      html  css  js  c++  java
  • 到达型01背包---P1504 积木城堡

    P1504 积木城堡

    题解

    到达型01背包

    对于每一组城堡,它可以到达一些高度

    但是我们要求的是所有背包可以到达的公共高度的最大值

    f[ i ] 表示对于一组城堡,能否到达高度 j ,然后我们跑 n 遍

    g[ i ] 表示对于所有城堡,能否到达高度 j 

    代码

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    #include<string>
    #include<cstring>
    #include<queue>
    
    using namespace std;
    
    typedef long long ll;
    
    inline int read()
    {
        int ans=0;
        char last=' ',ch=getchar();
        while(ch<'0'||ch>'9') last=ch,ch=getchar();
        while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
        if(last=='-') ans=-ans;
        return ans;
    }
    
    bool f[10005],g[10005];
    int n,a[10005],tot=0,sum=0,ans=0;
    
    int main()
    {
        n=read();
        for(int i=1;i<=10005;i++) g[i]=1;
        for(int t=1;t<=n;t++){
            tot=0;sum=0;
            memset(a,0,sizeof(a));
            memset(f,0,sizeof(f));
            while(a[++tot]=read()){
                if(a[tot]==-1) {
                    tot--;
                    break;
                }
                sum+=a[tot];
            }
            f[0]=1;
            for(int i=1;i<=tot;i++)
              for(int j=sum;j>=a[i];j--)
                 f[j]|=f[j-a[i]];
            for(int i=1;i<=10005;i++) g[i]=g[i]&f[i];
        }
        for(int i=0;i<=10005;i++) if(g[i]) ans=i;
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    HDU 2602
    ZOJ 1074 最大子矩阵和
    POJ 3903
    最大子段和
    HDU 1052
    CodeForces 18C
    CodeForces 546B-Soldier and Badges
    UVA 11462-Age sort
    Codeforces Round #326 (Div. 2)-Duff in Love
    Codeforces Round #327 (Div. 2)-Wizards' Duel
  • 原文地址:https://www.cnblogs.com/xiaoyezi-wink/p/12012444.html
Copyright © 2011-2022 走看看