Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- int main()
- {
- long n,m,i,key,leftRes=-1,rightRes=-1,first,last;
- long arr[100000];
- int mid;
- //freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- freopen("binsearch.in","r",stdin);
- freopen("binsearch.out","w",stdout);
- scanf("%i",&n);
- for(i=0; i<n; i++)
- scanf("%i",&arr[i]);
- scanf("%i",&m);
- for(i=0; i<m; i++)
- {
- scanf("%i",&key);
- first = 0;
- last = n-1;
- if (key<arr[first] || key>arr[last])
- {
- printf("-1 -1\n");
- }
- else
- {
- while (first < last)
- {
- mid = (first+last)/2;
- if (key <= arr[mid]) last = mid;
- else first = mid+1;
- }
- if ( arr[last] == key )
- {
- leftRes=last;
- while((leftRes>0)&&(arr[leftRes-1] == key)) leftRes--;
- rightRes=last;
- while((rightRes<n-1)&&(arr[rightRes+1] == key)) rightRes++;
- printf("%i %i\n",leftRes+1,rightRes+1);
- }
- else
- printf("-1 -1\n");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement