Author : Anwar Jahid Ruman Hossain
CSE, Batch - 6
BRUR.
Problem Statement : [ UVA ] 10611- The Playboy Chimp
Source : URI Online Judge
Category : Searching and Sorting
Algorithm : Binary Search
Verdict : Accepted
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long int
- int searchUpper(int key,int arr[],int low,int high){
- int mid,value=-1;
- while(low<=high){
- mid=(low+high)/2;
- if(key>=arr[mid]){
- low=mid+1;
- }else{
- high=mid-1;
- value=arr[mid];
- }
-
- }
- return value;
- }
- int searchDown(int key,int arr[],int low,int high){
- int mid,value=-1;
- while(low<=high){
- mid=(low+high)/2;
- if(key<=arr[mid]){
- high=mid-1;
- }else{
- value=arr[mid];
- low=mid+1;
- }
-
- }
- return value;
- }
- int main(){
-
-
- int n,q;
- while(cin>>n){
- int arr[n];
- for(int i=0;i<n;i++){
- cin>>arr[i];
- }
- cin>>q;
- int index;
- for(int i=0;i<q;i++){
- cin>>index;
- int up=searchUpper(index,arr,0,n-1);
- int down=searchDown(index,arr,0,n-1);
- if(down==-1)cout<<"X "<<up<<endl;
- else if(up==-1)cout<<down<<" X"<<endl;
- else cout<<down<<" "<<up<<endl;
- }
- }
- return 0;
- }
No comments:
Post a Comment