Ultraviolet (a) 10003-cutting sticks

Source: Internet
Author: User

Question: cut a piece of wood with a length of L into n + 1 segments. The cut cost is the length of the current segment.

Analysis: DP, range dynamic planning. The inverse process of stone merge.

State: If f (I, j) is cut from point I to J length into a corresponding small cost, there is a transfer equation:

F (I, j) = min (f (I, k) + f (K, j) + cost (I, j) {where I <k <j };

Note that the DP process is performed in ascending order of the interval size.

Note: Pay attention to the boundary condition. The price is 0 for only a short period of time.

# Include <iostream> # include <cstdlib> # include <cstring> using namespace STD; int cut [55]; int DP [55] [55]; int main () {int L, N; while (CIN> L> N) {for (INT I = 1; I <= N; ++ I) cin> cut [I]; cut [0] = 0; cut [n + 1] = L; // initialization, boundary condition for (INT I = 0; I <55; ++ I) for (Int J = 0; j <55; ++ J) DP [I] [J] = 1000000; For (INT I = 0; I <= N; ++ I) DP [I] [I + 1] = 0; // interval DP for (INT d = 2; D <= n + 1; ++ D) for (int s = 0; S + D <= n + 1; ++ s) for (int K = S + 1; k <S + D; ++ K) if (DP [s] [S + D]> DP [s] [k] + dp [k] [S + D] + cut [S + D]-cut [s ]) DP [s] [S + D] = DP [s] [k] + dp [k] [S + D] + cut [S + D]-cut [s]; cout <"The minimum cutting is" <DP [0] [n + 1] <". "<Endl;} return 0 ;}

Ultraviolet (a) 10003-cutting sticks

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.