Question: There is a string of numbers, and m different intervals are extracted from them. The length of each interval cannot exceed M, so that all numbers and the maximum values are obtained.
Analysis: DP, monotonous queue, maximum range field and. Because the data is positive and does not need to beMonotonous queue maintenance (otherwise use).
The maximum range field and, find the first K items and each element as the end mark; take the end position as the DP state;
Then, the Maintenance Interval Length of the monotonic queue is used, and the O (1) time is used to find the first J items that meet the minimum length and the difference is enough.
Status: set f (I, j) to the first J number, take the largest sum of the I ranges, and S (j) to the maximum single interval field and of the first J numbers;
Transfer: f (I, j) = max (f (I-1, k) + S (j) {where K ≤ j-m }.
(If the data can be negative and the equation remains unchanged, use a monotonic queue to calculate the maximum field sum of a single interval)
Note: The data is positive. You can use the simpler equation DP... )
#include <stdio.h>#include <stdlib.h>int main(){ int F[ 4 ][ 50005 ]; int T,N,M,t,i,m,l,r,s; while ( ~scanf("%d",&T) ) for ( t = 1 ; t <= T ; ++ t ) { scanf("%d",&N); for ( i = 1 ; i <= N ; ++ i ) scanf("%d",&F[ 0 ][ i ]); scanf("%d",&M); for ( s = 0,l = i = 1 ; i <= N ; ++ i ) { s += F[ 0 ][ i ]; if ( i-l >= M ) s -= F[ 0 ][ l ++ ]; F[ 1 ][ i ] = s; } for ( m = 2 ; m <= 3 ; ++ m ) for ( s = 0,i = 1 ; i <= N ; ++ i ) { if ( s < F[ m-1 ][ i-M ] ) s = F[ m-1 ][ i-M ]; F[ m ][ i ] = s + F[ 1 ][ i ]; } int max = 0; for ( i = 1 ; i <= N ; ++ i ) if ( max < F[ 3 ][ i ] ) max = F[ 3 ][ i ]; printf("%d\n",max); } return 0;}
Zoj 2501-A mini locomotive