POJ1976A Mini locomotive (01 back package + Continuous segment length)

Source: Internet
Author: User

A Mini locomotive
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 2485 Accepted: 1388

Description

A train has a locomotive this pulls the train with its many passenger coaches. If The locomotive breaks down, there is no-to-pull the train. Therefore, the Office of railroads decided to distribute three mini locomotives to each station. A mini locomotive can pull only a few passenger coaches. If a locomotive breaks down, three mini locomotives cannot pulling all passenger coaches. So, the office of railroads made a decision as follows:

1. Set the number of maximum passenger coaches a mini locomotive can pull, and a mini locomotive would not pull over the Nu Mber. The number is same to all three locomotives.
2. With three mini locomotives, let them transport the maximum number of passengers to destination. The office already knew the number of passengers in each passenger coach, and no passengers is allowed to move between co Aches.
3. Each mini locomotive pulls consecutive passenger coaches. Right after the locomotive, passenger coaches has numbers starting from 1.

For example, assume there is 7 passenger coaches, and one mini locomotive can pull a maximum of 2 passenger coaches. The number of passengers in the passenger coaches, in order from 1 to 7, is +, +,-60,

If three mini locomotives pull passenger coaches 1-2, 3-4, and 6-7, they can transport passengers. In this example, three mini locomotives cannot transport more than.

Given the number of passenger coaches, the number of passengers in each passenger coach, and the maximum number of Passeng ER coaches which can be pulled by a mini locomotive, write a program to find the maximum number of passengers which can is Transported by the three mini locomotives.

Input

The first line of the input contains a single integer t (1 <= T <= one), the number of test cases, followed by the Put data for each test case. The input for each test case would be as follows:
The first line of the input file contains the number of passenger coaches, which would not exceed 50,000. The second line contains a list of spaces separated integers giving the number of passengers in each coach, such that the I Th number of In this line are the number of passengers in Coach I. No coach holds more than passengers. The third line contains the maximum number of passenger coaches which can is pulled by a single mini locomotive. This number is not exceed 1/3 of the number of passenger coaches.

Output

There should be one line per test case, containing the maximum number of passengers which can is transported by the three Mini locomotives.

Sample Input

1735 40 50 10 30 45 602

Sample Output

240
Test instructions

There are three locomotives, n carriages, each of which corresponds to a certain number of seats. Each locomotive is required to pull up to a maximum of m consecutive carriages and the carriages they pull must be contiguous from left to right, asking the largest number of people it can pull;

Ideas:

Like the solution of the 01 knapsack, the first train to pull up m consecutive cars, here we put as long as the existence of a continuous m-compartment as an item. It is equivalent to the maximum amount of material that is placed in a backpack with a backpack capacity of 3. But notice here each successive M-compartment for an item, f[i][j] = max (F[i-1][j],f[i-m][j-1] + sum[i]-sum[i-m]); Here for each item is either not put, or is put (in a continuous M-car)

Sum[i] = a[0] + a[1] + ... + a[i];

Before seeing this problem-solving ideas feel a bit of doubt: there will be a repetition, the first section pulls 1, 2; the second section pulls 2,3 this, the final conclusion is not, because does not take this compartment, that must be "i-1" "J", if take words then certainly is "i-m" "j-1", jumped to i-m, So not heavy, too weak, in fact, the problem is quite simple, is not, weak

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 6 using namespacestd;7 Const intMAX =50000+Ten;8 intdp[max][5],sum[max],a[max];9 intMain ()Ten { One     intt,n,m; Ascanf"%d", &t); -      while(t--) -     { thescanf"%d", &n); -Memset (DP,0,sizeof(DP)); -memset (SUM,0,sizeof(sum)); -          for(inti =1; I <= N; i++) +scanf"%d", &a[i]); -          for(inti =1; I <= N; i++) +Sum[i] = sum[i-1] +A[i]; Ascanf"%d", &m); at         intTP; -          for(inti =1; I <= N; i++) -         { -              for(intj =1; J <=3; J + +) -             { -                 if(I < m)//because the most is the M section, less than M is also possible, need to deal with in                 { -TP =0; to                 } +                 Else -TP = i-m; theDP[I][J] = max (Dp[i-1][J], Dp[tp][j-1] + Sum[i]-SUM[TP]); *             } $         }Panax Notoginsengprintf"%d\n", dp[n][3]); -     } the     return 0; +}
View Code

POJ1976A Mini locomotive (01 back package + Continuous segment length)

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.