【UVA - 10474 】Where is the Marble?(排序)

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[10005];
int sum=0;
int main()
{
    while(cin>>N>>Q,N+Q)
    {
        sum++;
        ME0(a);
        for(int i=0; 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+1);
            else
                printf("%d not found\n",num);
        }
    }

}

原文地址:https://www.cnblogs.com/sky-stars/p/10988482.html

时间: 2024-09-29 08:30:20

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

uva 10474 Where is the Marble?(排序)

uva 10474 Where is the Marble? 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.

UVA - 10474 - Where is the Marble? (基数排序)

UVA - 10474 Where is the Marble? Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them. At the beginni

UVA 10474 - Where is the Marble?

Where is the Marble?  Where is the Marble?  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 writ

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

我很奇怪为什么要把它归类到回溯上,明明就是简单排序,查找就OK了,wa了两次,我还很不解的怀疑了为什么会 wa,原来是我竟然把要找的数字也排序了,当时只是想着能快一点查找,所以就给他排序了,没考虑到要按给的顺序输 出答案,这次真是二了,,,看别人题解有用打表做的,那个应该是正确解法,我的耗时980ms,估计数据再大一些就 要TLE了 贴代码: #include<stdio.h> #include<string.h> #include<stdlib.h> int cmp(

uva 10474 Where is the Marble?[ vector ]

思路:sort+low_bound()二分 #include<vector> #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<set> using namespace std; int main() { vector<int>v; int n,m; int cas=1; while(scanf("

UVA 10474:Where is the Marble?(STL初步)

 Where is the Marble?  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 Mee

uva 10026 Shoemaker&#39;s Problem(贪心+排序)

虽然是个水题,但是在一些细节上wa了几次,好像不支持'\b'退格符号,我用在了输出空格那,结果wa了...白白 wa了几次...题意是看的题解..今天只写了两道题,速度有点慢,得加快了,以后得先认真读懂题目,题目读懂了 就相当于做出来一半然后仔细动脑想想,有想法了再敲,不能盲目的做题.另外,热烈祝贺今天c++ primer看到 了100页 思路: 这道题是让给的数据是每件工作需要做的天数和每耽误一天所需要的费用,让求一个序列使得付费最小,如果有相同答 案把字典树最小的输出...输出的是序号,该件

uva 10905 Children&#39;s Game(排序或者有点贪心)

今天配置vim没有成功,老是显示什么error,唉,其实之前成功过的,只不过是重装了dev,然后就变了,可能环境 变量的问题,但是我都改了的啊,以后再调吧... 这道题其实不是我想出来的看的题解,又看题解了...好吧,既然看了题解就得好好掌握才是.用到了我刚刚在 c++ primer里面学的string类,挺好用的,以后我准备写程序尽量用c++内容,多练练.. 又加深理解了qsort调用的cmp函数,它的两个参数其实都是要比较的元素的指针,比如这道题中的元素是string类 型,那么他们都是st

uva:10763 - Foreign Exchange(排序)

题目:10763 - Foreign Exchange 题目大意:给出每个同学想要的交换坐标 a, b 代表这位同学在位置a希望能和b位置的同学交换.要求每一位同学都能找到和他交换的交换生. 解题思路:把给定的原先给定的序列,交换前后位置后得到新的序列.如果这个新的序列和原来的序列相同就符合要求.因为  (a,b) (b, a)若是成对出现,那么前后交换后还是(b, a)(a,b). 代码: #include <stdio.h> #include <string.h> #inclu

UVa 872 - Ordering 输出全拓扑排序

本题要求输出全部拓扑排序的序列. 还好本题的数据量不是很大,限制在26个大写英文字母,故此可以使用递归法输出. 这个递归输出全部解在Leetcode很多这样的题目的,不小心的话,还是很难调试的. 总体考了递归和拓扑排序,还有判断是否可以拓扑排序-就是是否图有环. 考了三大知识点,难度还是有的.因为数据量不大,故此判断环可以使用一般递归方法,递归只需要注意细节就好了. #include <stdio.h> #include <vector> #include <string.h