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.

Source: Internet
Author: User
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
  1. /**
  2. * The problem:
  3. * 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 ).
  4. * Use binary search, perhaps you shoshould compile it with-STD = c99
  5. * Fairywell 2011.
  6. */
  7. # Include <stdio. h>
  8. # Define max_num (1u <31)
  9. Int
  10. Main ()
  11. {
  12. Int I, n, low, high, mid, Max;
  13. Printf ("input How many numbers there are :");
  14. Scanf ("% d/N", & N );
  15. /* A [] holds the numbers, B [I] holds the number of increasing numbers
  16. * From a [0] to a [I], C [I] holds the number of increasing numbers
  17. * From a [n-1] to a [I]
  18. * Inc [] holds the increasing numbers
  19. * VLA needs c99 features, compile with-STC = c99
  20. */
  21. Double A [n], B [N], C [N], INC [N];
  22. Printf ("Please input the numbers:/N ");
  23. For (I = 0; I <n; ++ I) scanf ("% lf", & A [I]);
  24. // Update array B from left to right
  25. For (I = 0; I <n; ++ I) INC [I] = (unsigned) max_num;
  26. // B [0] = 0;
  27. For (I = 0; I <n; ++ I ){
  28. Low = 0; high = I;
  29. While (low
  30. Mid = low + (high-low) * 0.5;
  31. If (INC [Mid] <A [I]) Low = Mid + 1;
  32. Else high = mid;
  33. }
  34. B [I] = low + 1;
  35. INC [low] = A [I];
  36. }
  37. // Update array C from right to left
  38. For (I = 0; I <n; ++ I) INC [I] = (unsigned) max_num;
  39. // C [0] = 0;
  40. For (I = n-1; I> = 0; -- I ){
  41. Low = 0; high = I;
  42. While (low
  43. Mid = low + (high-low) * 0.5;
  44. If (INC [Mid] <A [I]) Low = Mid + 1;
  45. Else high = mid;
  46. }
  47. C [I] = low + 1;
  48. INC [low] = A [I];
  49. }
  50. Max = 0;
  51. For (I = 0; I <n; ++ I)
  52. If (B [I] + C [I]> MAX) max = B [I] + C [I];
  53. Printf ("% d Number (s) shocould be erased at least./N", N + 1-max );
  54. Return 0;
  55. }

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.

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.