Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define DIM 100005
- int a[DIM], lung[DIM], best[DIM];
- int n, max;
- void Read()
- {
- int i;
- scanf("%d", &n);
- for (i = n; i; --i)
- scanf("%d", &a[i]);
- }
- void Print() { /// afisrea cerintei
- unsigned l, index;
- scanf("%u", &l);
- while (l) {
- scanf("%u", &index);
- printf("%d\n", best[index + 2]);
- l--;
- }
- }
- int Binary_search(int st, int dr, int x)// cautare binarea alementelor din vectorul lungime care e sortat
- {
- int mij;
- for (; st <= dr; )
- {
- mij = (st + dr) / 2;
- if (a[lung[mij]] >= a[x] && (a[lung[mij + 1]] < a[x] || mij + 1 > max))
- return mij;
- else if (a[lung[mij]] >= a[x])
- st = mij + 1;
- else
- dr = mij - 1;
- }
- return 0;
- }
- void Solve()
- {
- int i, j;
- for (i = 1; i <= n; ++i)
- {
- j = Binary_search(1, max, i);
- if (j == max || a[i] >= a[lung[j + 1]]) // pentru ca scrie crescator inseamna ca poate fi si egal
- {
- lung[j + 1] = i;
- best[n - i + 1] = j + 1;
- if (j + 1 > max) max = j + 1;
- }
- }
- max = best[n];
- for (i = n - 1; i; --i) //pentru anumite subsiruri mai raman in best maxime locale
- if (best[i] < max)
- best[i] = max;
- else
- max = best[i];
- Print();
- }
- int main()
- {
- freopen("in.txt", "r", stdin); // numa ce am aflat de freopen si mi se par folositoare
- freopen("out.txt", "w", stdout); // setez toate operatile citire si scriere din fiserele respective fisere
- Read();
- Solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment