Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- int vvod (int x)
- {
- cout.flush();
- int y;
- cout << "?" << " " << x << endl;
- cin >> y;
- return y;
- }
- int bin (int znach, int left, int right)
- {
- int i = 0;
- int index_elem = -1;
- int middle;
- while (left < right)
- {
- i = (left + right) / 2;
- middle = vvod (i);
- if (middle < znach)
- left = i+1;
- else right = i;
- }
- middle = vvod (right);
- if (middle == znach)
- index_elem = right;
- return index_elem;
- }
- int poisk (int raznica, int left, int right)
- {
- int index = 0;
- int i=(right + left)/2;
- int middle = 0;
- middle = vvod (i);
- int tek_raznica = middle - i;
- if (tek_raznica < raznica)
- {
- right = i;
- if (right - left == 1)
- index = left;
- else
- return poisk (raznica, left, right);
- }
- if (tek_raznica >= raznica)
- {
- left = i;
- if (right - left == 1)
- index = left;
- else
- return poisk (raznica, left, right);
- }
- return index;
- }
- int main()
- {
- cout.flush();
- int n = 0, m = 0, first_elem, last_elem, middle, past_middle, index, znach, k;
- cin >> n >> m;
- cin >> znach;
- first_elem = vvod(1);
- last_elem = vvod (n);
- if (last_elem > first_elem)
- index = n;
- else
- {
- int raznica = first_elem - 1;
- index = poisk (raznica, 1, n);
- middle = vvod (index);
- }
- if (znach > middle)
- k = -1;
- else if (znach == first_elem)
- k = 1;
- else if (znach == last_elem)
- k = n;
- else if (index != n && znach < last_elem)
- k = bin (znach, index+1, n);
- else if (znach > first_elem)
- k = bin (znach, 1, index);
- else if (index == n && znach < last_elem && znach > first_elem)
- k = bin (znach, 1, n);
- cout << "!" << " " << k << endl;
- for (int i = 1; i< m; i++)
- {
- int znachenie;
- cin >> znachenie;
- if (znachenie > middle)
- k = -1;
- else if (znachenie == first_elem)
- k = 1;
- else if (znachenie == last_elem)
- k = n;
- else if (index != n && znachenie < last_elem)
- k = bin (znachenie, index+1, n);
- else if (znachenie > first_elem)
- k = bin (znachenie, 1, index);
- else if (index == n && znach < last_elem && znach > first_elem)
- k = bin (znachenie, 1, n);
- cout << "!" << " " << k << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement