# (Skill-based DP) Luo Gu P1070 road games (increase + / provincial election -)

Title Description

Little new is playing a simple computer game.

The game has a ring road, the road there are  n- n-robots plants, plant between two adjacent robots connected by a short road. The new factory with a small robot as a starting point, these clockwise order of  the n- the n-th Robot Factory No. 1-the n- 1 - the n-, because the road is circular, so the first the n- the n-th robot factory and 1 a robot plant is connected by a section of the road together. The new robot factory small connecting n pieces of the road which is also numbered  . 1-n . 1 - n, and a predetermined first i bin i connected to the i-th road and the robot factory  i. 1 + i + . 1 Factory robot ( 1 ≦ i ≦ -. 1 n . 1 I n - . 1), of the  n segments of the road connecting the first n  n n factories and robots . 1 a robot factory.

During the game, per unit time, each number will appear on the road some gold coins, gold coins will change over time, that is, the number of coins in different units of time occurs on the same period of the road may be different. We need help small new robot to collect gold coins on the road. The robot must be used in the desired number of coins to purchase factory robot, the robot is purchased once, it will have to walk clockwise along the circular road, travel once per unit time, i.e. the plant from the robot reaches the phase current location o next robot factory, and the collected after all the coins to the small new road, for example, in a small new I I ( ≦ i ≦ n . 1 I n-) No. robot factory purchased a robot, the robot I will from  i start number i robot factory, traveling clockwise on the road, will travel through the first i i road number, arrival  i + 1 i + 1 robot factory (if  i = n- i = n-, the robot will reach the first 1 a robot factory), and i collect all the gold coins to a small new road on the number i. Game, not co-exist on the ring road 2 2 or  2 having two or more robots, and each robot can walk up the road to the annular P P times. While a small purchase new robot, the robot needs to set the number of walking, running times can be  1 ~ p1 any integer between p. When the number of times the robot to walk on the road will go away completely specified, must immediately on any new small robot factory to buy a new robot, and the robot is set to a new number of new walking. 

Here are some additional description of the game:

  1. New game for the first time to buy small robot starts.

  2. Walking robot and set the number of purchase robot is instantaneous, without time-consuming.

  3. Purchase of robots and robot walks are two separate processes can not be purchased robot when the robot walks, completed the purchase of the robot and the robot walks after setting the number of robots to walk.

  4. Spent in the same robot factory robot buy is the same, but the cost of buying a robot in different robot factory is not necessarily the same.

  5. Buy gold robot spent at the end of the game then grew new collection of gold coins deducted, so in the course of the game need not worry due to lack of new small gold, not buy a robot which led to the game can not proceed. Because of this, after the end of the game, the number of coins collected may be negative.

Now the number of coins per unit time appeared on the road and buy known each segment in each robot factory robots need to spend, you tell little new, after  m after m time units, after deducting the cost of buying a robot, a small new up to collect the number of gold coins.

Input Format

The first line  . 3 three positive integers n-, m, P n- , m , P, as the meaning of the subject.

The next n- n-rows, each row having  m m a positive integer, separated by a space between each of two integers, where the i-th row description

Described the  i number of coins occurs in time for each unit road number i ( 1 ≦ . 1 ≤ number of coins ≦ 100 . 1 0 0), ie  i i on line  J J ( 1≤j≤m . 1 J m) represents the number of  j number of coins appear on the j-th unit road number at time i.

The last line, there are  n- n-integers, each separated by a space between two integers, wherein the first i i represents the number i for later robot takes a number of coins (i robot Factory No. 1 ≦ . 1 ≤ number of coins ≤100 . 1 0 0).

Output Format

A total line, comprising . 1 an integer representing the m the m units of time, the robot later be deducted

After spending the coins, a small number of new up to collect coins.

Sample input and output

Input # 1
2 3 2 
1 2 3 
2 3 4 
1 2
Output # 1
5

Description / Tips

【data range】

For 40% of the data, 2≤n≤40,1≤m≤40 2 n- . 4 0 , . 1 m . 4 0.

For 90% of the data, 2≤n≤200,1≤m≤200 2 n- 2 0 0 , . 1 m 2 0 0.

To 100% of the data, 2≤n≤1000,1≤m≤1000,1≤p≤m 2 n- . 1 0 0 0 , . 1 m . 1 0 0 0 , . 1 P m.

NOIP 2009 universal groups fourth title

Analysis: considering the maximum coins: for this ring, consider first breaking open, put him into a length of 2 * n sequence;

Consider the state of maintenance methods: first, to maintain a position coordinate that the current factory in which a robot;

Secondly, it must maintain the current time. Then the number of also storing coins; there are a few steps p robot

So disposed DP [i] [j] represents a j reaches the maximum number of coins at the factory time i;

For dp [i] [j], which may be transferred from the former 1-p plants over!

即:dp[i][j]=max(dp[i-1][j-1],dp[i-2][j-2],dp[i-3][j-3],...dp[i-p][j-p])+money[i][j];

Then consider Initialization: dp [1] [j] = money [j] [1] -cost [j];

Other initialization is 0;

(Error !)

Correct answer: Https://Www.Luogu.Org/problemnew/solution/P1070

 

/ *
1 to f [i] [j], set the maximum number of coins which arrive j at the i-th time;
R & lt [i] [j] indicates the j-th value of the i-th arrival time of the plant;
SUM [i ] [j] denotes the prefix value and that is to reach the number of gold coins at the factory i and j time!
Because there are two constraints of time and the amount of the plant, and requires the use of two-dimensional prefix!
sum [i] [j] -sum [i-1] [j-1] = r [i] [j];
so, in fact, each read, and maintains the prefixes are updated sum in the same diagonal array!
So for: [i] sum [j] -sum [ik] [jk] is equivalent to: r [ik + 1] [ jk + 1] + r [ik + 2] [jk +2]
+ .... + r [i] [J]; that is: go after jk ik factory in time, longer time k, t minutes to go, then
so should the current value corresponding to r [i- k + t] [jk + t ];
equivalent to storage start from the beginning away from the ik jk at the time, until the time k, i come at
the maximum value that can be obtained!

* /
#Include <cstdio>
#include <algorithm>
the using namespace STD;
int n-, m, P;
int SUM [1002] [1002], R & lt [1002] [1002], cost [1002], DP [1002];
int max inline (X int, int Y)
{
return X> Y X:? Y;
}
inline int Read ()
{
Char CH;
the while ((CH = getchar ()) < '0' || CH> '. 9');
int RES = CH-'0 ';
the while ((CH = getchar ())> =' 0 '&& CH <= '. 9')
RES = RES * 10 + CH-'0 ';
return RES;
}
int main ()
{
int T;
n-= Read (), m = Read (), P = Read ();
for ( int i = 1; i <= n; i ++) // read the i-th number of coins in the j-th path timing!
for (int j = 1; j <= m; j ++) // define the i-th road link i and i + 1, if i = n, is connected N->. 1;
{
Scanf ( "% D", & R & lt [ I] [J]); //
}
for (int I =. 1; I <= n-; I ++) // read each manufacturing plant consumes robot
scanf ( "% d", & cost [i]); //
for (int i = 1; i <= m; i ++) // initialize dp [i] array is the minimum value
{//
dp [i] = - 1E9; //
}
int ANS;


{
ANS = -cost [J] + DP [-I. 1]; // DP [-I. 1] max is a value on the moment, certain properties to meet the optimal substructure
for (int k = 0; k <p && i + k <= m; k ++) // performed for all the walk DP
{
T = J + K> n-(J + K)% n-:? J + K; // of a ring is treated
ans + = r [t] [i + k]; // plus the value of i + k t is time went
dp [i + k] = max (dp [i + k], ans); // get the maximum value!
}
}
The printf ( "% D", DP [m]); // Maximum Output Value!
0 return;
}

 

Guess you like

Origin www.cnblogs.com/little-cute-hjr/p/11440588.html