Alx09

Untitled

Apr 29th, 2020
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. #include <stdio.h>
  2. #define DIM 100005
  3. int a[DIM], lung[DIM], best[DIM];
  4. int n, max;
  5. void Read()
  6. {
  7. int i;
  8. scanf("%d", &n);
  9. for (i = n; i; --i)
  10. scanf("%d", &a[i]);
  11. }
  12. void Print() { /// afisrea cerintei
  13. unsigned l, index;
  14. scanf("%u", &l);
  15. while (l) {
  16. scanf("%u", &index);
  17. printf("%d\n", best[index + 2]);
  18. l--;
  19. }
  20. }
  21.  
  22. int Binary_search(int st, int dr, int x)// cautare binarea alementelor din vectorul lungime care e sortat
  23. {
  24. int mij;
  25. for (; st <= dr; )
  26. {
  27. mij = (st + dr) / 2;
  28. if (a[lung[mij]] >= a[x] && (a[lung[mij + 1]] < a[x] || mij + 1 > max))
  29. return mij;
  30. else if (a[lung[mij]] >= a[x])
  31. st = mij + 1;
  32. else
  33. dr = mij - 1;
  34. }
  35. return 0;
  36. }
  37. void Solve()
  38. {
  39. int i, j;
  40. for (i = 1; i <= n; ++i)
  41. {
  42. j = Binary_search(1, max, i);
  43. if (j == max || a[i] >= a[lung[j + 1]]) // pentru ca scrie crescator inseamna ca poate fi si egal
  44. {
  45. lung[j + 1] = i;
  46. best[n - i + 1] = j + 1;
  47. if (j + 1 > max) max = j + 1;
  48. }
  49. }
  50. max = best[n];
  51. for (i = n - 1; i; --i) //pentru anumite subsiruri mai raman in best maxime locale
  52. if (best[i] < max)
  53. best[i] = max;
  54. else
  55. max = best[i];
  56. Print();
  57.  
  58. }
  59. int main()
  60. {
  61. freopen("in.txt", "r", stdin); // numa ce am aflat de freopen si mi se par folositoare
  62. freopen("out.txt", "w", stdout); // setez toate operatile citire si scriere din fiserele respective fisere
  63. Read();
  64. Solve();
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment