我認為劉汝佳在《算法競賽入門經典》一書中的題目分類有錯~這題怎么被歸為暴力求解了呢?
題目大意:給出n個正整數,每個數都小于等于10000;有m次提問,問題是x在這個序列中第幾大,如果x不在序列中,輸出不在序列中。
以下是我的代碼:
#include<stdio.h>
#include<string.h>
int main()
{
/*
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
//*/
const long maxn=10008;
long n,m,test=0,be[maxn],pos[maxn];
while(scanf("%ld%ld",&n,&m)==2)
{
if(n==0||m==0) break;
test++;
printf("CASE# %ld:\n",test);
memset(be,0,sizeof(be));
memset(pos,0,sizeof(pos));
for(long i=1;i<=n;i++)
{
long t;
scanf("%ld",&t);
be[t]++;
}
// Read In
long tmp=0;
for(long i=0;i<maxn;i++)
if(be[i])
{
pos[i]=tmp+1;
tmp+=be[i];
}
// Init
for(long i=1;i<=m;i++)
{
long t;
scanf("%ld",&t);
if(pos[t])
printf("%ld found at %ld\n",t,pos[t]);
else printf("%ld not found\n",t);
}
}
return 0;
}
posted on 2010-01-09 19:22
lee1r 閱讀(845)
評論(0) 編輯 收藏 引用 所屬分類:
題目分類:基礎/模擬