Alx09

Untitled

Apr 17th, 2020
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #define DIM 100005
  3. int a[DIM], lung[DIM], best[DIM];
  4. int n, max, poz;
  5. void read()
  6. {
  7. int i;
  8. scanf("%d", &n);
  9. for (i = n; i; --i)
  10. scanf("%d", &a[i]);
  11. }
  12.  
  13. int cbin(int st, int dr, int x)
  14. {
  15. int mij;
  16. for (; st <= dr; )
  17. {
  18. mij = (st + dr) / 2;
  19. if (a[lung[mij]] >= a[x] && (a[lung[mij + 1]] <= a[x] || mij + 1 > max))
  20. return mij;
  21. else if (a[lung[mij]] >= a[x])
  22. st = mij + 1;
  23. else
  24. dr = mij - 1;
  25. }
  26. return 0;
  27. }
  28. void solve()
  29. {
  30. int i, j;
  31. for (i = 1; i <= n; ++i)
  32. {
  33. j = cbin(1, max, i);
  34. if (j == max || a[i] >= a[lung[j + 1]])
  35. {
  36. lung[j + 1] = i;
  37. best[n - i + 1] = j + 1;
  38. if (j + 1 > max) max = j + 1;
  39. }
  40. }
  41. max = best[n];
  42. for (i = n - 1; i; --i)
  43. if (best[i] < max)
  44. best[i] = max;
  45. else
  46. max = best[i];
  47. for (i = 1; i <= n; i++)
  48. printf("%d\n", best[i]);
  49.  
  50. }
  51. int main()
  52. {
  53. freopen("in.txt", "r", stdin);
  54. freopen("out.txt", "w", stdout);
  55. read();
  56. solve();
  57. return 0;
  58. }
Add Comment
Please, Sign In to add comment