UVA10474 Where is the Marble?

题意

PDF

分析

排序+二分查找模拟即可。

时间复杂度\(O(T \times(n+q) \times \log n)\)

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<ctime>
#include<cstring>
#define rg register
#define il inline
#define co const
template<class T>il T read()
{
    rg T data=0;
    rg int w=1;
    rg char ch=getchar();
    while(!isdigit(ch))
    {
        if(ch=='-')
            w=-1;
        ch=getchar();
    }
    while(isdigit(ch))
    {
        data=data*10+ch-'0';
        ch=getchar();
    }
    return data*w;
}
template<class T>T read(T&x)
{
    return x=read<T>();
}
using namespace std;
typedef long long ll;

co int N=1e4;

int main()
{
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    int n,q,x,a[N],kase=0;
    while(scanf("%d%d",&n,&q)==2&&n)
    {
        printf("CASE# %d:\n",++kase);
        for(int i=0;i<n;++i)
            scanf("%d",a+i);
        sort(a,a+n);
        while(q--)
        {
            scanf("%d",&x);
            int p=lower_bound(a,a+n,x)-a;
            if(a[p]==x)
                printf("%d found at %d\n",x,p+1);
            else
                printf("%d not found\n",x);
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/autoint/p/10092228.html
今日推荐