[cf148e] Porcelain (Group backpack)

Source: Internet
Author: User

Title Link: Http://codeforces.com/problemset/problem/148/E

The main topic: There are n groups of data, each can be removed from any of the two ends of a group of 1 number, ask you to take M number maximum composition how much?

Idea: First of all, the N group of data into each group of the maximum number of choices I can take, is to synthesize several items, and then the group knapsack problem.

Group Backpack:

Problem
There are n items and a backpack with a capacity of V. The cost of article I is c[i], the value is w[i]. These items are divided into groups in which the items in each group clash with each other, with a maximum of one item selected. The solution of which items are loaded into the backpack allows the sum of the costs of these items to be no more than the backpack capacity and the maximum value.
Algorithm
This problem becomes a group of items with a number of strategies: choose one of the groups or not. That is, set f[k][v] means the first K-Group Items cost V can obtain the maximum weight value, there are:
f[k][v]=max{f[k-1][v],f[k-1][v-c[i]]+w[i]| Item I belongs to Group K}
The pseudo-code for using a one-dimensional array is as follows:
For all Groups K
For V=v. 0
For all I belong to Group K
F[v]=max{f[v],f[v-c[i]]+w[i]}
Note that the order of the three-layer loops here, even in the first beta version of this article, I wrote the wrong one myself. "For V=v." 0 "This layer cycle must be outside of" for all I belong to Group K ". This ensures that only one of the items in each group will be added to the backpack.
In addition, it is obvious that "a simple and effective optimization" can be applied to the items within each group of P02.

1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <vector>5#include <map>6#include <Set>7#include <bitset>8#include <cmath>9#include <numeric>Ten#include <iterator> One#include <iostream> A#include <cstdlib> -#include <functional> -#include <queue> the#include <stack> -#include <list> - using namespacestd; - #definePB push_back + #defineMP Make_pair - #defineSZ size () + #defineST begin () A #defineED End () at #defineCLR Clear () - #defineZERO (x) memset ((x), 0,sizeof (x)) -typedefLong LongLL; -typedef unsignedLong LongULL; -typedef pair<int,int>PII; - Const DoubleEPS = 1e-8; in  - Const intMax_n =111; to intn,m; + intG[max_n][max_n]; - intA[max_n]; the intdp[max_n*Max_n]; *  $ voidProcessmaxsumwithrow (intRowintnum) {Panax Notoginseng     intSumfront[max_n],sumback[max_n]; - ZERO (sumfront); the ZERO (sumback); +      for(intI=1; i<=num;i++){ ASumfront[i] = sumfront[i-1] +A[i]; the     } +      for(inti=num;i>=1; i--){ -sumback[num-i+1] = Sumback[num-i] +A[i]; $     } $      for(intI=1; i<=num;i++){ -          for(intL=0; l<=i;l++){ -             intR = Il; theG[row][i] = max (g[row][i],sumfront[l]+sumback[r]); -         }Wuyi     } the } -  Wu intMain () { -scanf"%d%d",&n,&m); About      for(intI=1; i<=n;i++){ $         intnum; -scanf"%d", &g[i][0]); -          for(intj=1; j<=g[i][0];j++){ -scanf"%d",&a[j]); A         } +Processmaxsumwithrow (i,g[i][0]); the     } -      for(intk=1; k<=n;k++){ $          for(intv=m;v>=0; v--){ the              for(intI=1; i<=g[k][0];i++)if(v>=i) { theDP[V] = max (dp[v],dp[v-i]+g[k][i]); the             } the         } -     } inprintf"%d\n", Dp[m]); the     return 0; the}

[cf148e] Porcelain (Group backpack)

Related Article

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.