HDU - 4422 The Little Girl who Picks Mushrooms

题意:有5个背包,分别装着5座山上采的蘑菇,已知在其中n(1<=n<=5)座山上采的蘑菇数量,剩下的5-n座未知,采完后,要求有3个背包的蘑菇重量正好是1024的整数倍,求另外两个背包重量和不断被拿走1024直至重量不超过1024后的最大值。

分析:剩下的5-n座未知是解题关键。背包容量为2012。显然,最终的背包重量和最多为1024。

1、n=1,剩下的4个未知包中拿出两个,和这个已知的包凑成1024的整数倍,另外两个凑成1024。

2、n=2,用一个未知的包和这两个已知的包凑成1024的整数倍,另外两个凑成1024。

3、n=3,用一个未知的包和其中两个已知的包凑成1024的整数倍,用剩下的已知包和另外一个未知包凑成1024。

4、n=4,

(1)若其中三个已知包是1024的整数倍,则另外一个已知包一定能与剩下的未知包凑成1024。

(2)若任何三个已知包都不是1024的整数倍,则任选两个已知包与剩下的未知包凑成1024的整数倍,取剩下的两个已知包的重量和经不断拿走1024后的最大值。

5、n=5,完全按题意即可。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 10000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[10];
vector<int> v;
int tot;
int judge(int id){
    int len = v.size();
    int sum = 0;
    for(int i = 0; i < len; ++i){
        sum += a[v[i]];
    }
    if(id == 0){
        if(sum % 1024 != 0) return -1;
    }
    if((tot - sum) % 1024 == 0 && (tot - sum) != 0){
        return 1024;
    }
    return (tot - sum) % 1024;
}
int solve(int n, int len, int id){
    int ans = -1;
    for(int i = 0; i < (1 << n); ++i){
        v.clear();
        for(int j = 0; j < n; ++j){
            if(i & (1 << j)){
                v.push_back(j);
            }
        }
        if(v.size() == len){
            ans = max(ans, judge(id));
        }
    }
    return ans;
}
int main(){
    int n;
    while(scanf("%d", &n) == 1){
        tot = 0;
        for(int i = 0; i < n; ++i){
            scanf("%d", &a[i]);
            tot += a[i];
        }
        if(n <= 3){
            printf("1024\n");
            continue;
        }
        int ans = 0;
        if(n == 4){
            ans = solve(n, 3, 0);
            if(ans != -1) ans = 1024;
            else{
                ans = solve(n, 2, 1);
            }
        }
        else if(n == 5){
            ans = solve(n, 3, 0);
            if(ans == -1) ans = 0;
        }
        printf("%d\n", ans);
    }
    return 0;
}

  

posted @ 2017-07-25 10:46  Somnuspoppy  阅读(178)  评论(0编辑  收藏  举报