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