SHOW:
|
|
- or go back to the newest paste.
1 | - | 100 100 |
1 | + | #include <iostream> |
2 | - | 2 |
2 | + | #include <algorithm> |
3 | - | 2 |
3 | + | #include <vector> |
4 | - | 1 |
4 | + | #include <map> |
5 | - | 1 |
5 | + | |
6 | - | 2 |
6 | + | using namespace std; |
7 | - | 1 |
7 | + | |
8 | - | 1 |
8 | + | int binary_search(vector<int> marble, int size, int key) |
9 | - | 1 |
9 | + | { |
10 | - | 1 |
10 | + | int begin = 0; |
11 | - | 1 |
11 | + | int end = size; |
12 | - | 2 |
12 | + | int middle; |
13 | - | 2 |
13 | + | while (begin <= end) { |
14 | - | 2 |
14 | + | middle = (begin + end) / 2; |
15 | - | 2 |
15 | + | if (marble[middle] < key) { |
16 | - | 2 |
16 | + | begin = middle + 1; |
17 | - | 2 |
17 | + | } else if (marble[middle] > key) { |
18 | - | 2 |
18 | + | end = middle - 1; |
19 | - | 1 |
19 | + | } else { |
20 | - | 2 |
20 | + | return middle; |
21 | - | 1 |
21 | + | } |
22 | - | 2 |
22 | + | } |
23 | - | 1 |
23 | + | return -1; |
24 | - | 1 |
24 | + | } |
25 | - | 2 |
25 | + | |
26 | - | 1 |
26 | + | void testing(vector<int> marble, vector<int> tests, int n_of_tests, int n, int loop){ |
27 | - | 1 |
27 | + | cout << "CASE# " << loop << ":" << endl; |
28 | - | 2 |
28 | + | for(int i = 0; i < n_of_tests; i++){ |
29 | - | 1 |
29 | + | int aux = tests[i]; |
30 | - | 1 |
30 | + | int flag = binary_search(marble,n,aux); |
31 | - | 2 |
31 | + | if(flag == -1){ |
32 | - | 2 |
32 | + | cout << tests[i] << " not found" << endl; |
33 | - | 1 |
33 | + | } else { |
34 | - | 2 |
34 | + | cout << tests[i] << " found at " << flag << endl; |
35 | - | 1 |
35 | + | } |
36 | - | 2 |
36 | + | } |
37 | - | 1 |
37 | + | } |
38 | - | 2 |
38 | + | void reading(int loop){ |
39 | - | 2 |
39 | + | vector<int> marble; |
40 | - | 2 |
40 | + | int n, n_of_tests, aux; |
41 | - | 1 |
41 | + | cin >> n >> n_of_tests; |
42 | - | 2 |
42 | + | if((n == 0) && (n_of_tests == 0)){ |
43 | - | 2 |
43 | + | return; |
44 | - | 1 |
44 | + | } |
45 | - | 2 |
45 | + | for(int i = 0;i < n; i++){ |
46 | - | 2 |
46 | + | cin >> aux; |
47 | - | 1 |
47 | + | marble.push_back(aux); |
48 | - | 2 |
48 | + | } |
49 | - | 2 |
49 | + | vector<int> tests; |
50 | - | 2 |
50 | + | for(int i = 0; i < n_of_tests; i++){ |
51 | - | 1 |
51 | + | cin >> aux; |
52 | - | 2 |
52 | + | tests.push_back(aux); |
53 | - | 1 |
53 | + | } |
54 | - | 1 |
54 | + | std::sort(marble.begin(),marble.end()); |
55 | - | 2 |
55 | + | // for(int i = 0; i < marble.size(); i++){ |
56 | - | 2 |
56 | + | // cout << marble[i] << endl; |
57 | - | 2 |
57 | + | // } |
58 | - | 2 |
58 | + | testing(marble,tests,n_of_tests,n,loop); |
59 | - | 2 |
59 | + | reading(loop+1); |
60 | - | 2 |
60 | + | } |
61 | - | 1 |
61 | + | |
62 | - | 1 |
62 | + | int main(){ |
63 | - | 2 |
63 | + | reading(1); |
64 | - | 1 |
64 | + | return 0; |
65 | - | 1 |
65 | + | } |