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. Android studio 升级,不用下载完整版,完美更新到2.0

    Android studio 2.0 公布已有一旦时间,据说,速度大大提高了.但是一直没有尝试更新,看到大家相继更新,所以迫不及待就准备更新,但是.更新之路确实异常坎坷.询问度娘,千奇百怪的问题接憧而 ...

  2. 基本SQL 语句操作数据增删查改

    1.创建数据库: create database <数据库名>. 如:create database student; 2.连接到一个已经存在的数据库: use <数据库名>: ...

  3. centos下的hadoop集群实现ssh的无密码登陆

    CentOS 下SSH无密码登录的配置 最近学习Hadoop.它要求各节点之间通过SSH无密码登录,配置SSH的时候费了一番功夫,记录下来,以备忘. 配置SSH无密码登录需要3步: 1.生成公钥和私钥 ...

  4. struts(转)

    配置文件的优先级 在struts2中一些配置(比如常量)可以同时在struts-default.xml(只读性),strtus-plguin.xml(只读性),struts.xml,struts.pr ...

  5. hdu 3746 Cyclic Nacklace (KMP求最小循环节)

    //len-next[len]为最小循环节的长度 # include <stdio.h> # include <algorithm> # include <string. ...

  6. Python 模块的安装与使用

    我们知道使用函数不仅减轻了工作量,而且使代码更加简洁,更加的易于维护.但如果在另一个文件中,我们希望使用上一个文件中定义的某个函数,我们应该怎么办呢?我们需要重新将上一个函数再次实现一遍吗?而且,当我 ...

  7. 李洪强iOS开发之 - block的使用

     李洪强iOS开发之 - block的使用   01 - 定义block //支付block typedef void(^invokePayBlock)(CFBaseOrderModel *model ...

  8. Django之cookie 和 session

    一. 1.cookie的由来!!! 由于HTTP协议是无状态的,既每一次的请求都是独立的,他不会因为你之前来过,就记住你,所以每次浏览器去访问服务器的时候,都是一个全新的过程,之前的数据也不会保留,所 ...

  9. 编译spark源码 Maven 、SBT 2种方式编译

    由于实际环境较为复杂,从Spark官方下载二进制安装包可能不具有相关功能或不支持指定的软件版本,这就需要我们根据实际情况编译Spark源代码,生成所需要的部署包. Spark可以通过Maven和SBT ...

  10. searchkick HasMany Associations | index related model fields

    Project < ActiveRecord::Base searchkick has_many :categories_has_projects has_many :categories, t ...