Where is the Marble?

Descriptions:

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 Rajuto find the first marble with a certain number. She would count 1...2...3. Raju gets one point for correct answer, and Meena gets the point if Raju fails. 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. Each test case consists begins with 2 integers: N the number of marbles and Q the number of 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. Following Q lines will have Q queries. Be assured, none of the input numbers are greater than 10000 and none of them are negative.

Input is terminated by a test case where N = 0 and 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 different 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 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:

2 not found

3 found at 3

题目链接:

https://vjudge.net/problem/UVA-10474

题目大意就是现有N个大理石,每个大理石上写了一个非负整数。首先把各数从小到大排序,然后回答Q个问题。每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上写着x。排序后的大理石从左到右编号为1~N。

简单用两个函数 sort和lower_bound 即可

AC代码

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define ll long long
#define INF 0x3f3f3f3f
#define ME0(x) memset(x,0,sizeof(x))
using namespace std;
int N,Q;
int a[];
int sum=;
int main()
{
while(cin>>N>>Q,N+Q)
{
sum++;
ME0(a);
for(int i=; i<N; i++)
cin>>a[i];
sort(a,a+N);//排序
printf("CASE# %d:\n",sum);
while(Q--)
{
int num;
cin>>num;
//lower_bound(起始地址,结束地址,要查找的数值)
//地址:前闭后开。返回的是第一个大于或等于数值出现的位置
//如果数值大于数组中全部元素,返回的是结束地址。
int t=lower_bound(a,a+N,num)-a;
if(a[t]==num)
printf("%d found at %d\n",num,t+);
else
printf("%d not found\n",num);
}
} }

【UVA - 10474 】Where is the Marble?(排序)的更多相关文章

  1. UVA.10474 Where is the Marble ( 排序 二分查找 )

    UVA.10474 Where is the Marble ( 排序 二分查找 ) 题意分析 大水题一道.排序好找到第一个目标数字的位置,返回其下标即可.暴力可过,强行写了一发BS,发现错误百出.应了 ...

  2. uva 10474 Where is the Marble? 计数排序

    题目给出一系列数字,然后问哪个数字是从小到大排在第几的,重复出现算第一个. 数据范围为10000,不大,完全可以暴力,sort不会超时. 但是由于以前做比赛时也遇到这种题目,没注意看数据范围,然后暴力 ...

  3. UVa 10474 Where is the Marble

    题意:给出一列数,先排序,再查找学习了sort函数,lower_bound函数sort:可以给任意对象排序(包括自己定义的)(前提是定义好了‘<’运算符)lower_bound:查找大于或者等于 ...

  4. uva 10474 Where is the Marble?(简单题)

    我非常奇怪为什么要把它归类到回溯上,明明就是简单排序,查找就OK了.wa了两次,我还非常不解的怀疑了为什么会 wa,原来是我居然把要找的数字也排序了,当时仅仅是想着能快一点查找.所以就给他排序了,没考 ...

  5. UVA 10474 - Where is the Marble?--vector

    https://vjudge.net/problem/UVA-10474 https://blog.csdn.net/xiyaozhe/article/details/81081344 简单用法 so ...

  6. UVa10474 Where is the Marble?(排序sort)

    今天开始学STL,这是书上的一道例题,主要是用了sort函数和lower_bound函数,挺容易理解的. lower_bound的作用是查找“大于或等于x的第一个位置”. 需要注意的是,不要忘记alg ...

  7. 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 th ...

  8. 大理石在哪?(Where is the Marble?,UVa 10474)

    参考:ACM紫书 第五章 P108 [排序与检索] 下面的代码中有些 提示性输出,想Ac 需删除提示性输出语句,读者自行修改. #include <cstdio> #include < ...

  9. 大理石在哪儿(Where is the Marble?,Uva 10474)

    现有N个大理石,每个大理石上写了一个非负整数.首先把各数从小到大排序,然后回 答Q个问题.每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上 写着x.排序后的大理石从左到右编号为1 ...

随机推荐

  1. onedrive实现excel在线编辑 online excel

    1.首先用火狐浏览器或者谷歌浏览器登录 https://onedrive.live.com 2.注冊邮箱账户信息 3.在邮箱激活账户信息 4.登录进去.点击我的账户,点击左側文件树.点选上载,将文件上 ...

  2. Qt浅谈之二十一log调试日志

    一.简单介绍 近期因调试code时,想了解程序的流程,但苦于没有一个简易的日志记录,不停使用qDebug打印输出,而终于提交代码时得去多次删除信息打印,有时还会出现新改动的代码分不清是哪些部分.而使用 ...

  3. OpenStack源码系列---nova-compute

    nova-compute运行的节点为计算节点,虚拟机运行于计算节点上.例如对于创建虚拟机请求,nova-api接收到客户端请求后,经过nova-scheduler调度器调度,再将请求发送给某个选定的n ...

  4. linux 文件记录锁详解

    一: linux记录锁更恰当的称呼应该是范围锁,它是对文件某个范围的锁定. 关于记录锁的功能就是fcntl提供的第五个功能,具体使用如下: int fcntl(int fd, int cmd, str ...

  5. Yarn调度器负载模拟器——Yarn Scheduler Load Simulator (SLS)

    一.概述: Yarn调度器有很多实现,如Fifo, Capacity和Fair schedulers等.与其同一时候,正在进行一些优化措施来提高调度器在不同负载和工作场景下的性能.每一个调度器都有自己 ...

  6. 图像处理之基础---ffmpeg 中的图像缩放

    http://blog.csdn.net/bweaglegao/article/details/8540860 http://www.cnblogs.com/acloud/archive/2011/1 ...

  7. ul li menu

    第一步:建立一个无序列表我们先建立一个无序列表,来建立菜单的结构.代码是: <ul><li><a href="1">首页</a>&l ...

  8. Get Luffy Out (poj 2723 二分+2-SAT)

    Language: Default Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7969   ...

  9. SyntaxError:Strict mode does not allow function declaration in a lexically nested statement.

    问题描述   使用react-native init创建了一个新项目,在package.json中使用的react-native的版本如下: "dependencies": { & ...

  10. boogo08---中间件

    package main //中间件1:只允许特定host请求过来 import ( "fmt" "net/http" ) //SingleHost是一个中间件 ...