Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int zeroCase(int arr[], int key, int length)
- {
- if(arr[length-1] < key)
- return 0;
- int low = 0;
- int high = length-1;
- int mid;
- while(low+1 < high)
- {
- mid = (high+low)/2;
- if(arr[mid] >= key)
- high = mid;
- else if(arr[mid] < key)
- low = mid;
- }
- if(arr[low] >= key)
- return length-low;
- return length-high;
- }
- int firstCase(int arr[], int key, int length)
- {
- int low = 0;
- int high = length-1;
- int mid;
- while(low+1 < high)
- {
- mid = (low+high)/2;
- if(arr[mid] <= key)
- low = mid;
- else
- high = mid;
- }
- if(arr[low] > key)
- return length - low;
- if(arr[low] == arr[high])
- return 0;
- return length-high;
- }
- void quicksort(int x[10],int first,int last){
- int pivot,j,temp,i;
- if(first<last){
- pivot=first;
- i=first;
- j=last;
- while(i<j){
- while(x[i]<=x[pivot]&&i<last)
- i++;
- while(x[j]>x[pivot])
- j--;
- if(i<j){
- temp=x[i];
- x[i]=x[j];
- x[j]=temp;
- }
- }
- temp=x[pivot];
- x[pivot]=x[j];
- x[j]=temp;
- quicksort(x,first,j-1);
- quicksort(x,j+1,last);
- }
- }
- int main()
- {
- int length, i, test, type, check;
- scanf("%d",&length);
- int arr[length];
- for(i=0; i<length; i++)
- scanf("%d",&arr[i]);
- quicksort(arr,0,length-1);
- scanf("%d",&test);
- for(i=0; i<test; i++)
- {
- scanf("%d",&type);
- scanf("%d",&check);
- if(type == 0)
- printf("%d\n",zeroCase(arr,check,length));
- else if(type == 1)
- printf("%d\n",firstCase(arr,check,length));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement