Sha-10003 cutting sticks (recurrence)

Source: Internet
Author: User

Question: FIG (recurrence)


The length of a wooden stick is L. Now I want to saw the wooden stick and give n saw points. How can I minimize the overhead. For example, if a 10-bit wooden stick is sawing at 2 4 7, then in this order, the first result is that the wood with a length of 10 is sawing at 2 first, and the overhead is increased by 10, after the saw, there are two or eight wooden sticks with the lengths of [] and []. Now we need to saw the wood at the position of 4, in this way, we add 8 to the position of 4 in the wood with a length of 8, and then we have the wood with a length of [2, 4] [4, 10]. If we want to saw 7 at last, 6. therefore, the overhead is 10 + 8 + 6 = 24; if the order is 4 2 7: Overhead: 10 + 4 + 6 = 20; therefore, the minimum overhead is required.


Solution: In the previous idea, the length of wood is L. If we pick a local saw, we will generate the other two. In this way, we should start from the long. However, we later found that the push length from the short is the same, because the short composition is long, and the long saw is the same as the short one, at last, you only need to add the length (overhead) of the long wooden stick ). State transition equation: DP [I] [J]: represents the minimum overhead of the wood rod from saw point I to saw Point J. DP [I] [J] = min (DP [I] [k] + dp [k] [J] + J-I) k> I & K <j) the adjacent saw point indicates that the wooden stick cannot be saw, so DP [I] [I + 1] = 0;


Code:

# Include <cstdio> # include <cstring> typedef long ll; const int maxn = 1005; const int n = 55; const int INF = 0x3f3f3f; ll DP [maxn] [maxn]; int V [N]; int L, N; void Init () {v [n + 1] = L; V [0] = 0; For (INT I = 0; I <= N; I ++) DP [V [I] [V [I + 1] = 0;} ll min (const ll A, const LL B) {return a <B? A: B;} int main () {While (scanf ("% d", & L), L) {scanf ("% d", & N ); for (INT I = 1; I <= N; I ++) scanf ("% d", & V [I]); Init (); LL temp; for (INT I = 2; I <= n + 1; I ++) for (Int J = 0; j + I <= n + 1; j ++) {temp = inf; For (int K = 1; k <I; k ++) temp = min (temp, DP [V [J] [V [J + k] + dp [V [J + k] [V [J + I]); DP [V [J] [V [J + I] = temp + V [J + I]-V [J];} printf ("The minimum cutting is % LLD. \ n ", DP [0] [l]);} 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.