d732.
二分搜尋法
| From: [175.181.131.112] |
發表日期
:
2012-09-03 23:11
/**********************************************************************************/
/* Problem: d732 "二分搜尋法" from */
/* Language: CPP (682 Bytes) */
/* Result: AC(190ms, 602KB) judge by this@ZeroJudge */
/* Author: saitor362320 at 2012-09-03 22:50:18 */
/**********************************************************************************/
#include<cstdlib>
#include<iostream>
using namespace std;
int BinarySearch(int A[], int n, int k)
{
int l=0;
int u=n;
int m;
while(l<=u){
m = (l+u)/2;
if(k == A[m])
return m;
else if(k<A[m])
u=m-1;
else
l=m+1;
//cout << "l, m, u " << l << m << u << endl;
}
return -1;
}
int main()
{
int n, k;
while(cin>>n>>k){
int* num;
num = (int*) malloc(n*sizeof(int));
for(int i=0 ; i<n ; ++i){
cin >> num[i];
}
for(int i=0 ; i<k ; ++i){
int ask;
cin >> ask;
int ans = BinarySearch(num,n,ask);
if(ans<0)
cout << 0 << endl;
else
cout << ans+1 << endl;
}
}
return 0;
}