Advertisement
a53

SUBSIR_LUNGIME_MAXIMA_LOGARITMIC

a53
Mar 8th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. /// https://www.infoarena.ro/job_detail/146356?action=view-source
  2. /// https://www.infoarena.ro/problema/scmax
  3. #include <stdio.h>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. #define NMax 100005
  9.  
  10. int N, Res[NMax], v[NMax], lst[NMax], D[NMax], AIB[NMax], bst, up[NMax];
  11.  
  12. void update(int x, int ind)
  13. {
  14. for (; x <= N; x += x^(x-1) & x)
  15. if (D[ind] > D[AIB[x]])
  16. AIB[x] = ind;
  17. }
  18.  
  19. int query(int x)
  20. {
  21. int mx = 0;
  22.  
  23. for (; x; x -= x^(x-1) & x)
  24. if (D[AIB[x]] > D[mx])
  25. mx = AIB[x];
  26. return mx;
  27. }
  28.  
  29. int main(void)
  30. {
  31. int i, h = 1;
  32.  
  33. freopen("scmax.in", "r", stdin);
  34. freopen("scmax.out", "w", stdout);
  35.  
  36. scanf("%d", &N);
  37. for (i = 1; i <= N; ++i)
  38. {
  39. scanf("%d", &v[i]);
  40. Res[i] = lst[i] = v[i];
  41. }
  42.  
  43. sort(lst+1, lst+N+1);
  44. for (i = 2; i <= N; ++i)
  45. if (lst[i] != lst[h])
  46. lst[++h] = lst[i];
  47.  
  48. for (i = 1; i <= N; ++i)
  49. v[i] = lower_bound(lst+1, lst+h+1, v[i])-lst;
  50.  
  51. for (i = 1; i <= N; ++i)
  52. {
  53. up[i] = query(v[i]-1);
  54. D[i] = D[up[i]] + 1;
  55. update(v[i], i);
  56. }
  57.  
  58. for (i = 1; i <= N; ++i)
  59. if (D[bst] < D[i])
  60. bst = i;
  61.  
  62. printf("%d\n", D[bst]);
  63. for (h = 0, i = bst; i; i = up[i])
  64. lst[++h] = Res[i];
  65.  
  66. for (i = h; i; --i)
  67. printf("%d ", lst[i]);
  68. printf("\n");
  69.  
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement