当前位置: 首页 > 工具软件 > Marble > 使用案例 >

UVA 10474 - Where is the Marble?( 利用stl函数lower_bound)

幸弘扬
2023-12-01
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. 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
不知道stl有此函数的代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

int main()
{
    int n, q;
    int a[11000];
    int b[11000];
    int ans[11000] = {-1};
    int cnt = 1;
    while(scanf("%d %d", &n, &q) != EOF)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(ans,-1,sizeof(ans));
        if(!n&&!q)
            break;
        for(int i = 0; i < n; i ++)
        {
            scanf("%d", &a[i]);
        }
        sort(a, a + n);
        for(int i = 0; i < q; i ++)
        {
            scanf("%d", &b[i]);
        }
        for(int i = 0; i < q; i ++)
        {
            for(int j = 0; j < n; j ++)
            {
                if(a[j] == b[i]&&ans[i] ==-1)//注意次数如果用vist数组进行判断是否出现过,考虑该数是否超过所开vist数组大小。
                   {
                       ans[i] = j + 1;
                   }

            }
        }
        printf("CASE# %d:\n", cnt ++);
        for(int i = 0; i < q; i ++)
        {
            if(ans[i] != -1)
            printf("%d found at %d\n", b[i], ans[i]);
            else
                printf("%d not found\n", b[i]);
        }
    }

    return 0;
}
函数lower_bound()在first和last中的 前闭后开区间进行二分查找,返回大于或等于val的 第一个元素位置。如果所有元素都小于val,则返回 last的位置
注意,此数列应该是非递减数列。
#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int main()
{
    int a[5] = {1,3,2,3,4};
    int a1 = lower_bound(a,a + 5,4) - a;
    int a2 = lower_bound(a, a + 5, 10) - a;
    printf("a1 = %d\n", a1);
    printf("a2 = %d\n", a2);
    //result:
    //a1 = 4
    //a2 = 5
    return 0;
}
改进的算法不管是在空间上,还是在时间上都有很大的提升
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

int main()
{

    int n, q;
    int a[11000];
    int cnt = 1;
    while(scanf("%d %d", &n, &q) != EOF)
    {
        memset(a,-1,sizeof(a));
        if(!n&&!q)
            break;
        for(int i = 0; i < n; i ++)
        {
            scanf("%d", &a[i]);
        }
        sort(a, a + n);
        printf("CASE# %d:\n", cnt ++);
        for(int i = 0; i < q; i ++)
        {
            int tem;
            scanf("%d", &tem);
            int p = (lower_bound(a,a + n, tem) - a);
            if(a[p] == tem)
            {
                printf("%d found at %d\n", tem, p + 1);
            }
            else
            printf("%d not found\n", tem);
        }


    }

    return 0;
}



 类似资料: