《算法竞赛入门经典》 例题5-1 大理石在哪(Where is the Marble,UVa 10474)
原题及翻译
Raju and Meena love to play with Marbles.
拉朱和米娜喜欢玩弹珠。
They have got a lot of marbles with numbers written on them.
他们有很多写着数字的大理石。
At the beginning, Raju would place the marbles one after another in ascending order of the numbers written on them.
起初,拉朱会将大理石按数字的升序排列。
Then Meena would ask Raju to find the first marble with a certain number.
然后米娜会要求拉朱找到第一个有一定数量的大理石。
She would count 1…2…3.
她会数到1…2…3。
Raju gets one point for correct answer, and Meena gets the point if Raju fails.
拉朱的回答正确得1分,如果拉朱失败,米娜得1分。
After some fixed number of trials the game ends and the player with maximum points wins.
经过一定数量的测试后,游戏结束,得分最高的玩家获胜。
Today it’s your chance to play as Raju.
今天是你成为拉朱的机会。
Being the smart kid, you’d be taking the favor of a computer.
作为一个聪明的孩子,你会喜欢电脑。
But don’t underestimate Meena, she had written a program to keep track how much time you’re taking to give all the answers.
但不要低估米娜,她写了一个程序来跟踪你花了多少时间来给出所有的答案。
So now you have to write a program, which will help you in your role as Raju.
所以现在你必须写一个程序,这将有助于你作为拉朱的角色。
Input
输入
There can be multiple test cases.
可能有多个测试用例。
Total no of test cases is less than 65.
测试用例总数小于65。
Each test case consists begins with 2 integers: N the number of marbles and Q the number of queries Mina would make.
每个测试用例都由2个整数组成:n是大理石的数量,q是米娜要进行的查询的数量。
The next N lines would contain the numbers written on the N marbles.
接下来的n行将包含写在n个大理石上的数字。
These marble numbers will not come in any particular order.
这些大理石数字不会有任何特定的顺序。
Following Q lines will have Q queries.
以下Q行将有Q查询。
Be assured, none of the input numbers are greater than 10000 and none of them are negative.
请确保,输入的数字都不大于10000,也不为负数。
Input is terminated by a test case where N = 0 and Q = 0.
输入被一个n=0和q=0的测试用例终止。
Output
输出
For each test case output the serial number of the case.
对于每个测试用例,输出该用例的***。
For each of the queries, print one line of output.
对于每个查询,打印一行输出。
The format of this line will depend upon whether or not the query number is written upon any of the marbles.
这一行的格式将取决于查询号是否写在任何大理石上。
The two di?erent formats are described below:
两种不同的格式如下所述:
‘x found at y’, if the first marble with number x was found at position y.
“x在y处找到”,如果在y处找到第一个带有x号的大理石。
Positions are numbered 1, 2, . . . , N .
位置编号为1、2、。…n。
‘x not found’, if the marble with number x is not present.
?“X未找到”,如果不存在数字为X的大理石。
Look at the output for sample input for details.
有关详细信息,请查看示例输入的输出。
Sample Input
4 1
2
3
5
1
5
5 2
1
3
3
3
1
2
3
0 0
Sample Output
CASE# 1:
5 found at 4
CASE# 2:
2not found
3found at 3
思路
思路其实很简单,排序,然后查找,可以自己写函数来做这两件事,也可以直接用algorithm头文件中的函数来操作。
代码
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=10000;
int main ()
{
int n,q,x,a[maxn],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;
}
下一篇: 【个人邀请赛】洛谷 Math趣味赛