Repository navigation
Expand file tree
/
Copy path2_task.cpp
More file actions
82 lines (72 loc) · 1.67 KB
/
Copy path2_task.cpp
File metadata and controls
82 lines (72 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
using namespace std;
void expan(int arr1[],int arr2[],int size1,int size2,int &x,int &xmax,int &val,int i) {
val = arr2[i];
if (arr1[0] >= val || (arr1[0] <= val && arr1[1] >= val)) {
x = 0;
xmax = 1;
}
if (xmax != 1) {
for (int j = 1; j < size1; j *= 2) {
if (j * 2 >= size1) {
if ((val >= arr1[j]) && (val <= arr1[size1 - 1])) {
x = j;
xmax = size1 - 1;
break;
}
}
if ((val >= arr1[j]) && (val < arr1[j * 2])) {
x = j;
xmax = j * 2;
break;
}
}
}
}
int binarySearch(int arr[], int low, int high, int target)
{
int mid;
while (low < high)
{
mid = (low+high)/2;
if (arr[mid] >= target)
high = mid;
else
low = mid + 1;
}
if ( arr[low] >= target)
return low;
else
return -1;
}
int main() {
int size1=0;
int size2=0;
cin >>size1>>size2;
int *arr1=new int[size1];
int *arr2=new int[size2];
for (int i=0;i<size1;++i)
{
cin>>arr1[i];
}
for (int i=0;i<size2;++i)
{
cin>>arr2[i];
}
int x=0;
int xmax=0;
int val=0;
for (int i = 0; i <size2 ; ++i) {
expan(arr1,arr2,size1,size2,x,xmax,val,i);
x=binarySearch(arr1,x,xmax,val);
if (x==-1)
{
cout<<size1<<' ';
}
else
{cout<<x<<' ';}
x=0;
xmax=0;
}
return 0;
}