大理石在哪儿 (排序和查找) UVA

来源:互联网 发布:mac install ipython 编辑:程序博客网 时间:2024/04/30 14:11

题意:有n个数,现在给你一个x,问是不是存在x;

思路:用lower_bound返回第一个大于等于x的地址,如果相等,则存在;否则,不存在。

/*** Welcome To See My Code ***//***If I get TLE , it is good.If I get AC,it's NICE !***/#include <stdio.h>#include <iostream>#include <algorithm>#include <string.h>#include <vector>#include <cmath>#include <queue>#include <string>#include <map>using namespace std;typedef long long ll;const int INF=2147483647;const int MAXN=1e5+10;const ll mod=1e9+7;using namespace std;typedef long long ll;int a[MAXN];int main(void){    int n;    int q;    int kase=1;    while((scanf("%d%d",&n,&q))==2)    {        if(n==0 & q==0) break;        printf("CASE# %d:\n",kase);        kase++;        for(int i=1; i<=n; i++)            scanf("%d",&a[i]);        sort(a+1,a+1+n);        for(int i=1; i<=q; i++)        {            int x;            scanf("%d",&x);            int pos=lower_bound(a+1,a+1+n,x)-a;            if(a[pos]==x)                printf("%d found at %d\n",x,pos);            else                printf("%d not found\n",x);        }    }}

1.这题的思路在后面的比赛里我马上就碰到了。非常重要。

阅读全文
0 0
原创粉丝点击