Pku3378 crazy thairs-Dynamic Planning + tree Array

Source: Internet
Author: User

Description:

Given a numerical sequence {ai} with a length of N, ask how many ascending subsequences with a length of 5 in total. (AI <10 ^ 9, n <= 50000)

Analysis:

Because the number range is largeDiscretization1 ~ An integer between N. The original A [] array is converted to the D [] array, where D [I] = J indicates that a [I] is the number smaller than J in the [] array.

AvailableDynamic PlanningTo describe this algorithm:

P starts from 0 to N, And I = d [p].F [I] [k] indicates the number of Ascending Sequences ending with the number I and with the length k.

F [I] [k] + = Σ f [J] [k-1] (1 <= j <I)

F [I] [1] = 1

Such a dynamic transfer equation can be used to find the correct solution, but the time complexity isO (N ^ 2).

This can be used hereTree ArrayTo optimize the algorithm. We can see that what is required during status transfer is 1 ~ The sum of F [J] [I-1] Between K-1. Meets the requirements of the tree Array application. Modify the definition of F [I] [k] so that it is applied to a tree array. Reduced time complexityO (nlogn)

Note:1. The largest group of data should exceed int64, so it should be calculated using a large number. 2. the AI in the input data may have the same elements. You must handle them carefully.

 

  1. /*
  2. Pku3378 crazy thairs
  3. */
  4. # Include <stdio. h>
  5. # Include <stdlib. h>
  6. # Include <string. h>
  7. # Define clr (a) memset (a, 0, sizeof ())
  8. # Define int64 bignum_t
  9. # Define N 50005
  10. /*************************************** *************/
  11. # Define DIGIT 4 // number of digits stored by an array element
  12. # Define DEPTH 10000 // DEPTH = 10 DIGIT
  13. # Define MAX 30 // array Length
  14. Typedef int bignum_t [MAX + 1];
  15. // Assign a value
  16. Void value (bignum_t a, char buf []) {
  17. Char ch;
  18. Int I, j;
  19. Memset (void *) a, 0, sizeof (bignum_t ));
  20. For (a [0] = strlen (buf), I = a [0]/2-1; I> = 0; I --)
  21. Ch = buf [I], buf [I] = buf [a [0]-1-i], buf [a [0]-1-i] = ch;
  22. For (a [0] = (a [0] + DIGIT-1)/DIGIT, j = strlen (buf); j <a [0] * DIGIT; buf [j ++] = '0 ');
  23. For (I = 1; I <= a [0]; I ++)
  24. For (a [I] = 0, j = 0; j <DIGIT; j ++)
  25. A [I] = a [I] * 10 + buf [I * DIGIT-1-j]-'0 ';
  26. For (;! A [a [0] & a [0]> 1; a [0] --);
  27. }
  28. // Output
  29. Void write (const bignum_t ){
  30. Int I, j;
  31. For (printf ("% d", a [I = a [0]), I --; I --)
  32. For (j = DEPTH/10; j/= 10)
  33. Printf ("% d", a [I]/j % 10 );
  34. Puts ("");
  35. }
  36. // Addition
  37. Void add (bignum_t A, const bignum_t B) {// high-precision number A + B, the result is in
  38. Int I;
  39. For (I = 1; I <= B [0]; I ++)
  40. If (A [I] + = B [I])> = depth)
  41. A [I]-= depth, a [I + 1] ++;
  42. If (B [0]> = A [0])
  43. A [0] = B [0];
  44. Else
  45. For (; A [I]> = depth & I <A [0]; A [I]-= depth, I ++, a [I] ++ );
  46. A [0] + = (a [0] + 1]> 0 );
  47. }
  48. /*************************************** *****************/
  49. Int n, m;
  50. Int A [n], B [N], d [N];
  51. Int64 f [N] [5];
  52. Int cmp (const void * I, const void * j ){
  53. Return a [* (int *) I]-a [* (int *) j];
  54. }
  55. Void Add (int I, int j, int64 t ){
  56. While (I <= m ){
  57. Add (f [I] [j], t );
  58. I + =-I & I;
  59. }
  60. }
  61. Void Sum (int I, int j, int64 s ){
  62. Char c0 [10] = "0 ";
  63. Value (s, c0 );
  64. While (I> 0 ){
  65. Add (s, f [I] [j]);
  66. I-=-I & I;
  67. }
  68. }
  69. Int main ()
  70. {
  71. While (scanf ("% d", & N )! = EOF ){
  72. Int I, J, K;
  73. // Input
  74. For (I = 0; I <n; I ++ ){
  75. Scanf ("% d", & A [I]);
  76. B [I] = I;
  77. }
  78. // Order
  79. Qsort (B, n, sizeof (B [0]), CMP );
  80. M = 1;
  81. D [B [0] = 1;
  82. For (I = 1; I <n; I ++ ){
  83. If (A [B [I]! = A [B [I-1]) m ++;
  84. D [B [I] = m;
  85. }
  86. // DP
  87. Int64 V1, T, U;
  88. Char C0 [10] = "0 ";
  89. Char c1 [10] = "1 ";
  90. Value (V1, C1 );
  91. For (k = 0; k <= m; k ++) {// CLR
  92. For (j = 0; j <5; j ++ ){
  93. Value (F [k] [J], C0 );
  94. }
  95. }
  96. For (k = 0; k <n; k ++) {/dp
  97. I = d [k];
  98. Add (I, 0, V1); // F [I] [0] = 1
  99. For (j = 1; j <= 4; j ++ ){
  100. Sum (I-1, J-1, T); // F [J] T = sum (I-1)
  101. Add (I, j, T); // F [] [J] add (I, T)
  102. }
  103. }
  104. // Total
  105. Int64 total;
  106. Sum (m, 4, total );
  107. // Output
  108. Write (total );
  109. }
  110. Return 0;
  111. }

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.