鉴于NYOJ太卡,以后换个OJ刷题。
6 4 23 34 46 768 343 343 2 4 23 343
NO NO YES YES
分析:
哈希和二分查找都能AC,最初想直接申请一个100000000的数组,结果不允许。。。然后就有了下面的代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
int ans[1000010];
int main()
{
int n,m,i,k;
int low,mid,top;
// cin>>m>>n;
scanf("%d%d",&m,&n);
for(i=0;i<m;i++)
// cin>>ans[i];
scanf("%d",&ans[i]);
sort(ans,ans+m);
while(n--)
{
// cin>>k;
scanf("%d",&k);
low=0,top=m;
mid=(low+top)/2;
while(low<top)
{
if(k==ans[mid])
break;
if(k>ans[mid])
low=mid+1;
else
top=mid-1;
mid=(low+top)/2;
}
if(k==ans[mid])
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}原文地址:http://blog.csdn.net/u011694809/article/details/46514575