Problem: the number of columns is filtered out as few as possible so that we can see from left to right. These numbers are from small to large and then from large to small (Netease ).
Solution:This is a dual-end Lis problem. It can be solved using the DP idea. The objective planning function max {B [I] + C [I]}, where B [I] is left to right, 0 ~ The number of I numbers must increase progressively. C [I] is from right to left, n-1 ~ The number of I numbers must increase progressively. The final result is n-MAX + 1. In DP, an INC [] array can be maintained to represent an ascending number sequence. inc [I] is a number from small to large (this sentence can be understood as follows, INC [I] indicates the minimum end number of an ascending sequence when its length is I. For more information, see the above analysis on the longest descending subsequence ), then, when calculating B [I] C [I], use the binary search to locate the interval Inc [0] ~ in INC []. INC [I-1] is smaller than the number of elements in a [I] (low ).
Suppose it is an array arr [N], and its segment point is I (0-i increments, I to n-1 decreases). Suppose we use the method Lis (I) find the longest ascending subsequence from 0 to I, and find the longest descending subsequence from I to n-1, then its total length is Lis (I) + lDs (I)-1, so we can scan the entire array, that is, let I go from 0 to n-1, and find the largest Lis (I) + lDs (I)-1.
The source code is as follows:
[CPP]View plaincopy
- /**
- * The problem:
- * Remove as few numbers as possible from the number of columns so that you can see from left to right. These numbers are from small to large and then from large to small (Netease ).
- * Use binary search, perhaps you shoshould compile it with-STD = c99
- * Fairywell 2011.
- */
- # Include <stdio. h>
- # Define max_num (1u <31)
- Int
- Main ()
- {
- Int I, n, low, high, mid, Max;
- Printf ("input How many numbers there are :");
- Scanf ("% d/N", & N );
- /* A [] holds the numbers, B [I] holds the number of increasing numbers
- * From a [0] to a [I], C [I] holds the number of increasing numbers
- * From a [n-1] to a [I]
- * Inc [] holds the increasing numbers
- * VLA needs c99 features, compile with-STC = c99
- */
- Double A [n], B [N], C [N], INC [N];
- Printf ("Please input the numbers:/N ");
- For (I = 0; I <n; ++ I) scanf ("% lf", & A [I]);
- // Update array B from left to right
- For (I = 0; I <n; ++ I) INC [I] = (unsigned) max_num;
- // B [0] = 0;
- For (I = 0; I <n; ++ I ){
- Low = 0; high = I;
- While (low
- Mid = low + (high-low) * 0.5;
- If (INC [Mid] <A [I]) Low = Mid + 1;
- Else high = mid;
- }
- B [I] = low + 1;
- INC [low] = A [I];
- }
- // Update array C from right to left
- For (I = 0; I <n; ++ I) INC [I] = (unsigned) max_num;
- // C [0] = 0;
- For (I = n-1; I> = 0; -- I ){
- Low = 0; high = I;
- While (low
- Mid = low + (high-low) * 0.5;
- If (INC [Mid] <A [I]) Low = Mid + 1;
- Else high = mid;
- }
- C [I] = low + 1;
- INC [low] = A [I];
- }
- Max = 0;
- For (I = 0; I <n; ++ I)
- If (B [I] + C [I]> MAX) max = B [I] + C [I];
- Printf ("% d Number (s) shocould be erased at least./N", N + 1-max );
- Return 0;
- }
Remove as few numbers as possible from the number of columns so that you can see from left to right. These numbers are from small to large and then from large to small.