POJ no.1769

Source: Internet
Author: User

Category: DP | Segment Tree

Test instructions conversion: use m intervals to cover the 1~n, to find the minimum number of intervals required.

Analysis: Assuming that the number of input n is the maximum value that should be output, after the operation of the selected interval, if it can be moved from position I to nth, then maximizer normal operation. The analysis shows that if i = 1 o'clock Maxmizer can work properly, then I can work properly for any of them. So, suppose the first number of inputs is the maximum value that should be output, and then the DP solution is used.

Dp[i]:= the minimum number of intervals required to move the maximum from position 1 to position I.

Initialization

Dp[1] = 0 (no need to move the number of intervals required is 0)

Dp[i] = INF (i > 1)

Status update:

Dp[ti] = min (Dp[ti], dp[j] + 1) and Si<=j <= ti

Worst time complexity: O (MN)

Core Code implementation:

void Solve (int n, int m)//n number of M intervals

{

Fill (DP, DP + N + 1, INF);

DP[1] = 0;

for (int i = 0; I <= m; ++i)

{

for (int j = S[i]; J <= T[i]; ++J)

Dp[t[i]] = min (Dp[t[i]], Dp[j] + 1);

}

cout << Dp[n] << Endl;

}

The subject can be optimized using a segment tree: Find the minimum value of the interval (s[i], t[i]) query (S[i], t[i])

Optimized code implementation:

void Solve (int n, int m)

{//Time complexity O (MLOGN)

Fill (DP, DP + N + 1, INF);

DP[1] = 0;

BUILD_SBT (n);//construct segment tree

Update (1, 0);//position 1 0

for (int i = 0; i < m; ++i)

{

int v = min (dp[t[i]], query (S[i], t[i]));

Dp[t[i]] = v;

Update (T[i], V);

}

cout << Dp[t[i]] << Endl;

}

POJ no.1769

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.