九度oj 1349 数字在排序数组中出现的次数

原题链接:http://ac.jobdu.com/problem.php?pid=1349 
二分。。

 1 #include<algorithm>

 2 #include<iostream>

 3 #include<cstdlib>

 4 #include<cstdio>

 5 using std::lower_bound;

 6 using std::upper_bound;

 7 const int Max_N = 1000010;

 8 int arr[Max_N];

 9 int main() {

10 #ifdef LOCAL

11     freopen("in.txt", "r", stdin);

12     freopen("out.txt", "w+", stdout);

13 #endif

14     int n, m, v;

15     while (~scanf("%d", &n)) {

16         for (int i = 0; i < n; i++) scanf("%d", &arr[i]);

17         scanf("%d", &m);

18         while (m--) {

19             scanf("%d", &v);

20             printf("%d\n", upper_bound(arr, arr + n, v) - lower_bound(arr, arr + n, v));

21         }

22     }

23     return 0;

24 }
View Code

 

你可能感兴趣的:(排序)