Building Block Castle (Castle) (01 backpack)

Source: Internet
Author: User
Castle)
Time limit:
1000 ms
Memory limit:
102400kb
Description

There are N sequences that are not incrementing. Now we need to remove some of them to make the sum of each sequence as big as possible, and this sum should be as big as possible.


Input
The first line is an integer n (n <= 100), indicating a total of several castles. Each line in the N rows below is a series of non-negative integers separated by a space, and the edges of all blocks in a castle are given in sequence from bottom to top. End with-1. The number of blocks in a castle cannot exceed 100, and the length of each block cannot exceed 100.
Output
An integer that represents the maximum possible height of the final Castle. If no suitable solution is found, 0 is output.
Sample Input
2
2 1-1
3 2 1-1
Sample output
3

Question: Use a 01 backpack for each castle to find all the heights that meet the conditions. Then count the highest heights of all the castles.
#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;int h[101];bool dp[101][10001];int main(){    int n, ret;    while(scanf("%d",&n) != EOF)    {        ret = 10000;        int i, j, k, cnt, sum;        for(k = 0; k < n; k++)        {            cnt = sum = 0;            while(1)            {                scanf("%d",&h[cnt]);                if(h[cnt] == -1) break;                sum += h[cnt++];            }            dp[k][0] = true;            for(i = 0; i < cnt; i++)                for(j = sum; j >= h[i]; j--)                    dp[k][j] = (dp[k][j] || dp[k][j-h[i]]);            for(i = sum; !dp[k][i]; i--);            ret = min(ret, i);        }        for(i = ret; i >= 0; i--)        {            bool flag = true;            for(j = 0; j < n; j++)                if(dp[j][i] == false) {flag = false; break; }            if(flag == true) { ret = i; break;}        }        printf("%d\n",ret);    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.