Simple DP
Max_coach indicates the maximum number of vehicle cards that can be pulled by each small locomotive. Passenger [I] stores the number of consecutive max_coach vehicle cards from forward to facilitate calculation.
DP [x] [Y] indicates the maximum number of people that can be pulled after the y locomotive is used for the Section X card. The state transition equation is
DP [x] [Y] = max (DP [x-1] [Y], DP [x-max_coach] [Y-1] + passenger [x]);
DP [x-1] [Y] indicates that section X is not pulled, DP [x-max_coach] [Y-1] + passenger [x] indicates section X is pulled
/*************************************** **************************************** # Author: neo Fung # Email: neosfung@gmail.com # Last modified: 2012-02-24 # filename: zoj2501 poj1976 a mini locomotive. CPP # description: **************************************** **************************************/# ifdef _ msc_ver # define debug # DEFINE _ crt_secure_no_deprecate # endif # include <fstream> # include <stdio. h> # include <iostream> # include <string. h> # include <string> # include <limits. h> # include <algorithm> # include <math. h> # include <numeric> # include <functional> # include <ctype. h> # define Max 50010 using namespace STD; int DP [Max] [4]; int temp [Max], passenger [Max]; int main (void) {# ifdef debug freopen (".. /stdin.txt "," r ", stdin); freopen (".. /stdout.txt "," W ", stdout); # endif int ncases, N, max_coach; scanf (" % d ", & ncases); While (ncases --) {scanf ("% d", & N); For (INT I = 1; I <= N; ++ I) scanf ("% d ", & temp [I]); memset (DP, 0, sizeof (DP); memset (passenger, 0, sizeof (passet); scanf ("% d ", & amp; max_coach); For (INT I = 1; I <= N; ++ I) if (I> max_coach) passenger [I] = passenger [I-1] + temp [I]-Temp [i-max_coach]; elsepassenger [I] = passenger [I-1] + temp [I]; int sum = 0; for (INT I = 1; I <= N; ++ I) for (Int J = 1; j <= 3; ++ J) {if (I <= J * max_coach) DP [I] [J] = DP [I-1] [J] + temp [I]; elsedp [I] [J] = max (DP [I-1] [J], DP [i-max_coach] [J-1] + passenger [I]);} printf ("% d \ n", DP [N] [3]);} return 0 ;}