Poj 3093 inititas on the river walk (0-1 backpack deformation)

Source: Internet
Author: User
The idea of this topic is clever. Under what circumstances can all the remaining items be put down? That is, the smallest of the remaining items cannot be placed. Therefore, first sort items by capacity from small to large, and enumerate the situations where the current backpack is the smallest item that cannot be placed.

For the current item I, there must be 1 to all the items of the I-1 put in, this time than I large items who put who don't put is not sure. Convert to 0-1 backpack problem: Put the previous items in the I-1, get space for tsum-sum [I-1] (prefix and) package, as long as you fill out a scheme from the I + 1 to the N items so that the remaining volume is smaller than the volume of the I items, the total number of solutions is the result!

Note: In special cases, when the smallest item after sorting cannot be placed, 0 is returned directly, because DP [0] is initialized to 1,

Code:

/* Poj 3093232k0ms */# include <cstdio> # include <algorithm> # include <iostream> # define maxn 1005 using namespace STD; int n, m, wei [35], DP [maxn], sum [maxn] ;__ int64 bag () {memset (sum, 0, sizeof (SUM); sort (Wei + 1, wei + n + 1); If (wei [1]> m) // because there may be a minimum number of records, DP [0] is initialized to 1, 1 return 0; For (INT I = 1; I <= N; I ++) sum [I] = sum [I-1] + wei [I]; __int64 ans = 0; For (INT I = 1; I <= N; I ++) {If (sum [I-1]> m) break; memset (DP, 0, sizeof (DP); DP [sum [I-1] = 1; for (Int J = I + 1; j <= N; j ++) for (int K = m; k> = sum [I-1] + wei [J]; k --) DP [k] + = DP [k-wei [J]; for (Int J = m-wei [I] + 1; j <= m; j ++) ans + = DP [J];} return ans;} int main () {INT cases; CIN> cases; for (int cnt = 1; CNT <= cases; CNT ++) {CIN> N> m; For (INT I = 1; I <= N; I ++) CIN> wei [I]; cout <CNT <"" <bag () <Endl;} 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.