Turn off the light problem DP

Source: Internet
Author: User

Test instructions is a row of street lights, each street lamp has power consumption, illumination, need to give this n a street lamp in order to group, the maximum power consumption in each group is the number of lights in T, you can choose to turn off some of the lights, the maximum degree of illumination;

The idea is obvious, preprocessing a g[i][j] means I to j is divided into a group of the maximum illumination degree, f[i][j] means the first I is divided into J Group of the maximum illumination, F[i][j]=max (F[k-1][j-1]+f[k][i]);

That's what the naïve pretreatment is about.

1 inth[maxm*MAXN];2      for(intI=1; i<=n;i++)3          for(intj=i;j<=n;j++){4memset (H,0,sizeof(h));5             intS= (j-i+1)*T;6              for(intk=i;k<=j;k++)7                  for(intc=s;c>=0; c--){8                     if(C<w[k]) Break;9H[c]=max (h[c],h[c-w[k]]+v[k]);Ten                 } Oneg[i][j]=g[j][i]=H[s]; A}
View Code

N^4, unable to accept, observed, found that H array every time so clear again too wasted, to think how to get information from the previous H, found every h in the i-j of the best information, and then deal with the i-j+1 when the equivalent of processing again i-j, to find i-j+1 g value, Can consider not emptying the H array, directly from the j+1 up, but each time the maximum power consumption is not the same, directly can be set to i-n the maximum power consumption, and then after each processing, in the 1-current maximum power consumption to find the maximum value on the line, a one-dimensional province, you can pass;

To modify the code:

1#include <iostream>2#include <cstdio>3#include <string>4#include <cstring>5#include <algorithm>6#include <iomanip>7#include <cstdlib>8 using namespacestd;9 Const intmaxn= the, maxm= -;Ten intN,M,T,W[MAXN],V[MAXN]; One intG[MAXN][MAXN],F[MAXN][MAXM]; A voidinit () { -scanf"%d%d%d",&n,&m,&t); -      for(intI=1; i<=n;i++) scanf ("%d%d",&w[i],&v[i]); the     inth[maxn*MAXN]; -      for(intI=1; i<=n;i++){ -memset (H,0,sizeof(h)); -         intS= (n-i+1)*T; +          for(intj=i;j<=n;j++){ -             intS= (j-i+1)*T; +              for(intc=s;c>=0; c--){ A                 if(C<w[j]) Break; atH[c]=max (h[c],h[c-w[j]]+v[j]); -             } -g[i][j]=g[j][i]=H[s]; -         } -     } - } in voidWork () { -      for(intI=1; i<=n;i++) to          for(intk=1; k<=m&&k<=i;k++){ +              for(intj=k;j<=i;j++){ -F[i][k]=max (f[i][k],f[j-1][k-1]+g[j][i]); the             } *         } $cout<<f[n][m]<<Endl;Panax Notoginseng } - intMain () { the init (); + Work (); A}
View Code

Turn off the light problem DP

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.