DP Backpack Series __DP

Source: Internet
Author: User

One: 01 backpack

POJ 3624 Charm bracelet

The backpack doesn't have to be full.

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int mm=12889;
int dp[mm];
int n,m;
int main ()
{
  while (~scanf ("%d%d", &n,&m))
  {int a,b;
    Memset (Dp,0,sizeof (DP));
    for (int i=0;i<n;++i)
    {scanf ("%d%d", &a,&b);
     /** the reverse guarantees that each only has a single
     *
      /for (int i=m-a;i>=0;--i)
        Dp[i+a]=max (Dp[i]+b,dp[i+a]) selected;
    printf ("%d\n", Dp[m]);
  }

POJ 362 Bookshelf 2

What full backpack ... In fact, DFS search on the line, the data is only 20, I think each can reach the value of 1, more than the end of the update answer, with a linked list can not be traversed again.

This is the direct backpack method traversal, if plus and greater than 10^9 this method is not, all say the topic data water, another efficiency is not high


#include <cstdio>
using namespace std;
const int mm=20000000;
BOOL F[MM];
int h[22];
int i,j,k,n,b,m;
int main ()
{
    while (scanf ("%d%d", &n,&b)!=-1)
    {
        for (m=i=0;i<n;++i) scanf ("%d", &h[i) ), M+=h[i];
        for (I=0;i<=m;++i) f[i]=0;
        F[0]=1;
        for (I=0;i<n;++i) for
            (J=M-H[I];J>=0;--J)
                if (f[j]) f[j+h[i]]=1;
        for (I=b;i<=m;++i)
            if (f[i]) break;
        printf ("%d\n", i-b);
    }
    return 0;
}


Time complexity is O (n^2) n if it is 1000,dfs, it explodes.

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int mm=1e6+9;
const int OO=1E9;
int dp[mm],nex[mm];
int n,m;
int main ()
{
  while (~scanf ("%d%d", &n,&m))
  {int A;
    Memset (Dp,0,sizeof (DP));
    memset (nex,0,sizeof (NEX));
    int ans=oo,z;
    for (int i=0;i<n;++i)
    {scanf ("%d", &a);
      if (a>=m)
       {ans=min (ans,a-m);
         Continue;
       }
       z=0;
       for (int i=nex[z];i>0;i=nex[i])
       {
         if (i+a>=m) {ans=min (ans,i+a-m); continue;} The current Cow
         if (!dp[i+a]) dp[i+a]=1,nex[i+a]=nex[i],nex[i]=i+a,i=nex[i] on the local chain of all the values on the previous chain
       ;
       if (!dp[a]) dp[a]=1,nex[a]=nex[0],nex[0]=a;
    }
    printf ("%d\n", ans);
  }

POJ 3211 Washing Clothes

is also a backpack, can also use the above linked list optimization. Here the code is not optimized, a color of the total time and half of the backpack.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
const int mm=1e5+9;
string S;
map<string,int>mp;
int dp[14][mm],sum[14];
int n,m;
int main ()
{while
  (~scanf ("%d%d", &n,&m))
  {if (n==0&&m==0) break;
    Mp.clear ();
     Memset (Dp,0,sizeof (DP));
     memset (sum,0,sizeof (sum));
    for (int i=0;i<n;++i)
    {dp[i][0]=1;
      cin>>s;mp[s]=i;
    }
    int x,z;
    for (int i=0;i<m;++i)
    {
      scanf ("%d", &x);cin>>s;
      z=mp[s];sum[z]+=x;
      for (int j=sum[z];j>=x;--j)
        if (Dp[z][j-x])
          dp[z][j]=1;
    }
    int ans=0;
    for (int i=0;i<n;++i)///backpack for half the time of laundry
    {for
      (int j=sum[i]/2;j>=0;--j)
        if (dp[i][j))
        {
        ans+=sum[i]-j;break
        }
    }
    printf ("%d\n", ans);
  }


POJ 1745 divisibility

It is similar to the above backpack, but to understand a truth, if a few number of addition and subtraction operations can be divided by K, then the number of MoD K operation can still.

Then it becomes a backpack, see if you can finish after the MoD k is 0. I save space on the use of scrolling array, this problem seems to be able to use the linked list optimization, but the subject is not only 100 useful.


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
const int mm=110;
int dp[2][mm];
int n,k,z,a;
int main ()
{
  while (~scanf ("%d%d", &n,&k))
  {z=0;dp[0][0]=1;
    for (int i=0;i<n;++i)
    {
      scanf ("%d", &a);
      A= (a%k+k)%k;
      for (int i=0;i<k;++i)
        if (Dp[z][i])
      {dp[z][i]=0;///is initially 0, is the next
        dp[z^1][(i+a)%k]=1;
        dp[z^1][(i-a+k)%k]=1;
      }
      z^=1;
    }
    if (Dp[z][0]) printf ("divisible\n");
    else printf ("Not divisible\n");
  }


POJ 1976 A Mini locomotive

Backpack, the number of small locomotives as a different package, Dp[x][y] before the x front to Y can be transported to the maximum number of people.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
const int mm=5e4+9;
int dp[4][mm];
int f[mm];
int n,k,t;
int main ()
{while
  (~scanf ("%d", &t))
  {
    while (t--)
    {scanf ("%d", &n);
     f[0]=0;
     Memset (Dp,0,sizeof (DP));
     for (int i=1;i<=n;++i)
      {
        scanf ("%d", &f[i]); f[i]+=f[i-1];
      scanf ("%d", &k);
      for (int i=1;i<=3;i++)
      {for
        (int j=0;j<=n;++j)
          {
            Dp[i][j]=max (j?dp[i][j-1]:0,j-k>=0?) DP[I-1][J-K]+F[J]-F[J-K]:d p[i-1][0]+f[j]);
          }
      printf ("%d\n", Dp[3][n]);}}


POJ 2923 Relocation (dp+ State compression +01 backpack)

POJ 1837 Balance (DP 01 backpack)



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.