uva-10611 The Playboy Chimp Solution
#include<iostream>
using namespace std;
void binary_search(int *arr,int start,int end,int val,int &pos){
if (start == end){
if (arr[start] > val)
pos = start;
else
pos = -1;
return;
}
int mid = (start + end) / 2;
if (arr[mid] > val){
binary_search(arr, start, mid, val, pos);
}
else if (arr[mid + 1] > val){
pos = mid + 1;
}
else{
binary_search(arr, mid + 1, end, val, pos);
}
}
int main(){
int arr[50001], pos, x, i, m, n,z;
cin >> n;
cin >> arr[0];
z = 0;
for (i = 1; i < n; i++){
cin >> x;
if (arr[z] != x){
z++;
arr[z] = x;
}
}
cin >> m;
for (i = 0; i < m; i++){
cin >> x;
if (z == 0){
if (arr[0] == x)
cout << "X X" << endl;
else if (arr[0] < x)
cout << arr[0] << " X" << endl;
else
cout << "X " << arr[0] << endl;
}
else{
binary_search(arr, 0, z, x, pos);
if (pos == 0)
cout << "X " << arr[pos] << endl;
else if (pos == -1){
if (arr[z] == x)
cout << arr[z - 1];
else
cout << arr[z];
cout << " X" << endl;
}
else{
if (arr[pos - 1] == x){
if (pos - 2 >= 0)
cout << arr[pos - 2]<<" ";
else
cout << "X ";
}
else
cout << arr[pos - 1] << " ";
cout << arr[pos] << endl;
}
}
}
return 0;
}
using namespace std;
void binary_search(int *arr,int start,int end,int val,int &pos){
if (start == end){
if (arr[start] > val)
pos = start;
else
pos = -1;
return;
}
int mid = (start + end) / 2;
if (arr[mid] > val){
binary_search(arr, start, mid, val, pos);
}
else if (arr[mid + 1] > val){
pos = mid + 1;
}
else{
binary_search(arr, mid + 1, end, val, pos);
}
}
int main(){
int arr[50001], pos, x, i, m, n,z;
cin >> n;
cin >> arr[0];
z = 0;
for (i = 1; i < n; i++){
cin >> x;
if (arr[z] != x){
z++;
arr[z] = x;
}
}
cin >> m;
for (i = 0; i < m; i++){
cin >> x;
if (z == 0){
if (arr[0] == x)
cout << "X X" << endl;
else if (arr[0] < x)
cout << arr[0] << " X" << endl;
else
cout << "X " << arr[0] << endl;
}
else{
binary_search(arr, 0, z, x, pos);
if (pos == 0)
cout << "X " << arr[pos] << endl;
else if (pos == -1){
if (arr[z] == x)
cout << arr[z - 1];
else
cout << arr[z];
cout << " X" << endl;
}
else{
if (arr[pos - 1] == x){
if (pos - 2 >= 0)
cout << arr[pos - 2]<<" ";
else
cout << "X ";
}
else
cout << arr[pos - 1] << " ";
cout << arr[pos] << endl;
}
}
}
return 0;
}
Comments
Post a Comment