UVa 10474 Where is the Marble? (The use of binary lookup &equal_range ()

Source: Internet
Author: User
Tags cas printf time limit

10474-where is the Marble?

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_ problem&problem=1415

Raju and Meena with marbles. They have got a lot of marbles with numbers on written. At the beginning, Raju would place the marbles one over another in ascending order of the numbers on written. Then Meena would ask Raju to find the the ' the ' the ' the ' the ' the ' I Marble She would count 1...2...3. Raju gets one point for correct answer, and Meena gets the point if Raju. After some fixed number of trials the game ends and the player with maximum points wins. Today's 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 so much time for you ' re taking to give all the answers . So now you are have to write a, which'll help you in your 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 won't come in any particular order. Following q lines would have Q queries. Be assured, none of the input numbers are greater than 10000 and none of them are.

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

This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45365.htm

For each of the queries, print one line of output. The format of this line would 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-marble with number x is found at position y. Positions are numbered 1, 2,..., N.

' X not found ', if the marble with number X are not present.

Look in 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

Use Equal_range () to break.

Complete code:

/*0.118s*/
    
#include <cstdio>  
#include <algorithm>  
using namespace std;  
    
int num[10005];  
pair<int*, int*> bounds;  
    
int main ()  
{  
    int n, q, cas = 0, I, qu;  
    while (scanf ("%d%d", &n, &q), n | | | q)  
    {for  
        (i = 0; i < n; ++i)  
            scanf ("%d", &num[i]);  
        Sort (num, num + N);  
        printf ("case#%d:\n", ++cas);  
        while (q--)  
        {  
            scanf ("%d", &qu);  
            bounds = equal_range (num, num + n, qu);  
            if (Bounds.first = = Bounds.second) printf ("%d not found\n", qu);  
            else printf ("%d found at%d\n", Qu, bounds.first-num + 1);  
        }  
    return 0;  
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.