POJ-3612 telephone wire Dynamic Planning

Source: Internet
Author: User

The status of this question is very imaginative, and the code I wrote has forgotten how to write it ......

In fact, just like what I have done before, I want to grow tall. Given n numbers, we need to multiply the absolute values of the two consecutive numbers by the minimum value of C. Each number can become larger, the overhead of the variable is the square of the variable.

The state equation is DP [I] [J] = min (DP [I-1] [k] + C * | j-k | + (J-H [I]) ^ 2)

If (j> = k) DP [I] [J] = min (DP [I-1] [k]-C * K) + C * j + (J-H [I]) ^ 2
If (j <= k) DP [I] [J] = min (DP [I-1] [k] + C * K) -C * j + (J-H [I]) ^ 2

The State is to deduce the state of line I through the I-1 line, the trick here is

For (Int J = H [I-1]; j <= 100; ++ J) min = min (Min, DP [I-1] [J]-C * K)

This loop is used to traverse all the valid heights of the I-1 rows. In combination with the following statement, the K in the current j> min can be obtained.
That is, when the minimum value of Min is retained, K must be less than or equal to the J value that we have enumerated. This satisfies the first sub-statement, next, you only need to determine whether the current J can be obtained through H [I], that is, j> = H [I].

For (Int J = 100; j> = H [I]; -- j) min = min (Min, DP [I-1] [J] + C * K)

The boundary condition here is not H [I-1], because the number of states in the I-th row we requested should be from H [I]-100, so we need to control the boundary to H [I].

The Code is as follows:

# Include <cstdlib> # include <cstdio> # include <iostream> # include <cstring> # include <algorithm> # define INF 0x3f3f3f # define maxn 100000 using namespace STD; int N, c, seq [maxn + 5]; int DP [2] [105]; // DP [I] [J] indicates the minimum value of inline int pow2 (INT X) {return x * X;} int dp () when the height of the nth digit is J () {int min; memset (DP, 0x3f, sizeof (DP); For (Int J = seq [1]; j <= 100; ++ J) {// only DP [1] [J] = pow2 (J-seq [1]);} For (INT I = 2; I <= N; ++ I) {int T = I & 1; min = inf; For (Int J = 1; j <= 100; ++ J) DP [T] [J] = inf; // each time you need to initialize for (Int J = seq [I-1]; j <= 100; ++ J) {// enumerate the state of the previous layer to bring down the state of the current layer // enumerate the I-1 layer from small to large, the height of the I-1 layer corresponding to the saved values in Min must be smaller than J min = min (Min, DP [! T] [J]-C * j); If (j> = seq [I]) {DP [T] [J] = min (DP [T] [J], min + C * j + pow2 (J-seq [I]);} min = inf; For (Int J = 100; j> = seq [I]; -- j) {// ensure that all valid states are evaluated for Min = min (Min, DP [! T] [J] + C * j); If (j> = seq [I]) {DP [T] [J] = min (DP [T] [J], min-C * j + pow2 (J-seq [I]); }}min = inf; For (INT I = seq [N]; I <= 100; ++ I) {min = min (Min, DP [N & 1] [I]);} return min;} int main () {While (scanf ("% d", & N, & C) = 2) {for (INT I = 1; I <= N; ++ I) {scanf ("% d", & seq [I]);} printf ("% d \ n", dp ();} return 0 ;}

 

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.