Continuous backpack (Backpack)

Source: Internet
Author: User

[Problem description]
Select some items from the items in the T group and put them in the backpack to find the minimum value of the remaining space.
Restriction: You must select consecutive items from each group. That is to say, if there are n items in this group: item 1, Item 2, Item 3 ,... , Item n, you can only select item I, item I + 1 ,... , Item j, where 1 <= I <= j <= n, or not.
[Input]
The first two positive integers v and T separated by spaces. The number of spaces and items in the backpack.
Next there are T rows. Each row first has a positive integer ni, indicating that this group of items has ni, and then ni positive integers, indicating the size of each item.
[Output]
Only one number indicates the minimum value of the remaining space.
[Input and output sample]
Bag. in
100 3
3 7 6 8
2 80 70
4 101 108 103150
 
Bag. out
6
 
[Example of input and output]
Select 6, 8 for the 1st group, 80 for the 2nd group, and 3rd for the group.
[Restrictions]
60% of data meets the requirements: 1 <= ni <= 10
100% of data meets the requirements: 1 <= ni <=, 1 <= v <=, 1 <= T <= 10
 


For each section of a backpack
Next we will find our backpacks
Note that in a single step Vmax = V [I-1] + sum [ni]

[Cpp]
# Include <cstdio>
# Include <cstring>
# Include <cstdlib>
# Include <iostream>
# Include <cmath>
# Include <functional>
# Include <algorithm>
Using namespace std;
# Define MAXNI (100 + 10)
# Define MAXV (500000 + 10)
# Define MAXN (10 + 10)
Int T, n, v, sum [MAXNI] = {0}, a [MAXNI], A [MAXN] [MAXV] = {0 };
Bool f [MAXN] [MAXV] = {0 };
Int main ()
{
Freopen ("bag. in", "r", stdin );
Freopen ("bag. out", "w", stdout );
Scanf ("% d", & v, & T );
// V = v * 10;
F [0] [0] = 1;
For (int I = 1; I <= T; I ++)
{
Scanf ("% d", & n );
Sum [0] = 0;
For (int j = 1; j <= n; j ++) {scanf ("% d", & a [j]); if (a [j]> v/*/10 */) a [j] = 0; sum [j] = sum [J-1] + a [j];}
// For (int j = 1; j <= n; j ++) printf ("% d", sum [j]);
For (int k = 0; k <= v + sum [n]; k ++)
For (int j = n; j> = 0; j --)
{
If (k-sum [j] <0) continue;
If (f [I-1] [k-sum [j])
{
F [I] [k] = 1;
A [I] [k] = j;
Break;
}
}
For (int k = 0; k <= v + sum [n]; k ++)
For (int j = A [I] [k]-1; j> = 1; j --)
{
F [I] [k-sum [j] = f [I] [k] | f [I] [k-sum [j];
}



// Cout <endl;
}
// V/= 10;
Int I = v;
While (! F [T] [I])
{
I --;
}
Printf ("% d \ n", v-I );
/*
For (int I = 0; I <= T; I ++)
{
For (int j = 0; j <= v; j ++)
If (f [I] [j]) cout <I <',' <j <'';
Cout <'\ n ';
}

While (1 );
*/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.