The main topic: N Wood Long l_i heavy w_i, the former wood than the next one to waste a minute to prepare the machine, the least waste time.
Topic Link: Click to open the link
Analysis:
This involves the concept of partial ordering, that is, to make stick[i]>stick[j], you must satisfy L[I]>L[J] and w[i]>w[j].
In order to facilitate the comparison, we first put the L in ascending order, at this time just consider the relationship between W, and then converted into a few piles from the n wood, each heap to meet W are incremented, the minimum number of heaps.
First of all, consider the question, for n intervals, we are going to select a series of intervals from these n intervals and these intervals do not overlap, so at least a few times such operations can choose to complete all the intervals. Then it must be found that the maximum number of overlapping m, the M range must not be placed in the same series, so the minimum number of operations is M.
Back to think about this question, if we find w[] the longest descent subsequence length m, then this m w must not be placed in the same heap, analogy with the above-mentioned interval problem, you can yy out M is the final solution. (more abstract, must be carefully understood)
And then the LIS, and here are 2 ways to find out.
①dp[i] Indicates the length of the longest descent subsequence ending with W[i]
If W[i] is the largest element in the first I, then dp[i]=1
If there is k<i and W[k]>w[i], then dp[i]=dp[k]+1
Dp[i] = max (1,dp[k]) (where K satisfies k<i and W[k]>w[i])
Complexity of O (n^2)
② using stacks to optimize
This solution is not what I think, learned from the Internet, here to share with you. The complexity of this algorithm is only O (Nlogn), in the case of large amounts of data, the algorithm is very efficient ...
(Excerpt from the original author's words)
This algorithm is actually not DP, a bit like greed. As for the complexity of the reduction is actually because the algorithm used two points search. There would have been N number to deal with is O (n), each calculation to find N or O (n), a total of O (n ^ 2); Now search is replaced by O (logn) binary search, the overall complexity becomes O (NLOGN).
The exact operation of this algorithm is as follows (by Ryanwang):
Open a stack, each time the top element of the stack and read the element temp to compare, if the temp > top will be the temp stack, if the temp < top binary lookup stack in the 1th number of the larger than temp, and replace it with temp. The longest sequence length is the stack's size top.
This is also well understood, for X and Y, if x < Y and stack[y] < stack[x], replace Stack[y with Stack[x], at this time the longest sequence length has not changed but the "potential" of the sequence Q has increased.
Example: The original sequence is 1,5,8,3,6,7
Stack for 1,5,8, at this time read to 3, replace 5 with 3, get 1,3,8, read 6, replace 6 with 8, get 1,3,6, then read 7, get the final stack for 1,3,6,7. The longest increment subsequence is the length 4.
I think that when the 1,5,8,2 occurs, the last number in the stack is 1,2,8 is not the correct sequence. Is it wrong.
Analysis, we can see, although sometimes this does not get the correct sequence, but the number of the final figure is right, why?
Think, when temp>top, the total number directly add 1, this certainly is true; But when it comes to temp<top. At this point, temp must just replace an element inside the stack, so the size does not change, that is, an element less than the top of the stack when added, the total number is unchanged. The analysis of these two cases shows that the algorithm is more efficient if the number is only necessary. However, if a sequence is required to be printed, only DP is used.
We can use STL to do these things very easily.
Attached code:
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define L First
#define W second
int T, n, dp[5005]; Here is still a DP to express, essentially a stack
pair<int, int> sticks[5005];
int main ()
{
scanf ("%d", &t);
while (t--)
{
scanf ("%d", &n);
for (int i = 1; I <= n; i++)
scanf ("%d%d", &STICKS[I].L, &STICKS[I].W);
Sort (sticks + 1, sticks + n + 1);
Memset (DP,-1, sizeof DP); It can be assigned a value of-1 and then using <span style= "font-family:arial, Helvetica, Sans-serif;" >lower_bound implementation of the find operation, the first 1 position is the top of the stack, I have to imitate on the paper over the </span> for
(int i = 1; I <= n; i++)
*lower_bound ( DP, DP + N, STICKS[I].W, greater<int> ()) = STICKS[I].W; Find the position of the first <=w[i] and change its value
to printf ("%d\n", Lower_bound (DP, DP + N,-1, greater<int> ())-DP); The first <=-1 position is the stack top
}
return 0;