您的位置:首页 > 其它

UVa 10474 Where is the Marble?

2014-07-01 08:23 435 查看
典型的排序检索问题,需要注意的是返回排好序后要找的第一次出现的位置(序号是从1开始数的)。

开始不知道bsearch()函数,所以自己写了个二分查找,用来用bsearch也同样A过去了。

貌似自己写的比库函数还快0.001秒,嘎嘎!

Where is the Marble?

Raju and Meenalove to play with Marbles. They have got a lot of marbles with numbers writtenon them. At the beginning, Raju would place the marbles(弹子) one afteranother in ascending order(按升序排列)of the numberswritten on them. Then Meena would ask Raju to find the first marble with acertain number. She would count 1...2...3. Raju gets one point for correctanswer, and Meena gets the point if Raju fails. After some fixed number oftrials the game ends and the player with maximum points wins. Today it's yourchance to play as Raju. Being the smart kid, you'd be taking the favor of acomputer. But don't underestimate(低估) Meena, she hadwritten a program to keep track how much time you're taking to give all theanswers. So now you have to write a program, which will help you in your roleas Raju.

Input

There can bemultiple test cases. Total no of test cases is less than 65. Each test case consistsbegins with 2 integers: N the numberof marbles and Q the numberof queries(询问) Mina would make. The next N lines would contain the numbers written on the N marbles. These marble numbers will not come in any particular order.FollowingQ lines willhave Q queries. Beassured, none of the input numbers are greater than 10000 and none of them arenegative.

Input isterminated by a test case where N = 0 and Q = 0.

Output

For each test caseoutput the serial number of the case.

For each of thequeries, print one line of output. The format of this line will depend uponwhether or not the query number is written upon any of the marbles. The twodifferent formats are described below:

`x found at y', if the first marble with number x was found at position y. Positions are numbered 1, 2,..., N.

`x not found', if the marble with number x is not present.

Look at the outputfor sample input for details.

SampleInput

4 1

2

3

5

1

5

5 2

1

3

3

3

1

2

3

0 0

SampleOutput

CASE# 1:

5 found at 4

CASE# 2:

2 not found

3 found at 3

[align=center][/align]
Problem-setter:Monirul Hasan Tomal, Southeast University

AC代码:

//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 10000 + 5;
int a[maxn], b[maxn];
int cmp ( const void *a , const void *b );

int main(void)
{
#ifdef LOCAL
freopen("10474in.txt", "r", stdin);
#endif

int n, q, i, kase = 0;
while(scanf("%d %d", &n, &q) == 2 && (n + q))
{
printf("CASE# %d:\n", ++kase);
for(i = 0; i < n; ++i)
scanf("%d", &a[i]);
for(i = 0; i < q; ++i)
scanf("%d", &b[i]);
qsort(a, n, sizeof(int),cmp);
int pos, *p;
for(i = 0; i < q; ++i)
{
p = (int *)bsearch(&b[i], a, n, sizeof(int), cmp);
if(p != NULL)
{
pos = p - a;
int j = pos;
while(a[j] == a[pos] && j >= 0)//要找第一个数
--j;
printf("%d found at %d\n", b[i], j + 2);
}
else
printf("%d not found\n", b[i]);
}
}
return 0;
}
int cmp ( const void *a , const void *b )
{
return *(int *)a - *(int *)b;
}


代码君
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: