Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 133
- #include <iostream>
- #include <math.h>
- #include <string>
- using namespace std;
- int bsearch(int x[100000], int n, int k)
- {
- int g1, g2, gm;
- g1 = 0;
- g2 = n - 1;
- gm = (g1 + g2) / 2;
- while (g2 - g1 != 1)
- {
- if (k >= x[gm]) g1 = gm;
- else g2 = gm;
- gm = (g1 + g2) / 2;
- }
- if (x[g2] == k) return g2 + 1;
- if (x[g1] == k) return g1 + 1;
- return -1;
- }
- int main()
- {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- int x[100000];
- int n, i, k, m,z1,z2;
- cin >> n;
- for (i = 0;i < n;i++)
- cin >> x[i];
- cin >> m;
- for (i = 0;i < m;i++)
- {
- cin >> k;
- z1 = bsearch(x, n, k);
- z2 = z1;
- if (z1 == -1)
- cout << "Not found" << endl;
- else
- {
- while (x[z2 - 1] == x[z2 - 2])
- z2--;
- cout << z1-z2+1 << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement