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("%d%d",&n,&m),n||m)
    {
        int t;
        v.clear();
        for(int i=0;i<n;i++)
        {
            cin>>t;
            v.push_back(t);
        }
        sort(v.begin(),v.end());
        cout<<"CASE# "<<cas++<<":"<<endl;
        for(int i=0;i<m;i++)
        {
            cin>>t;
            int pos=lower_bound(v.begin(),v.end(),t)-v.begin();     //大于等于t的第一个元素
            if(v[pos]==t)
                cout<<t<<" found at "<<pos+1<<endl;
            else
                cout<<t<<" not found\n";
        }
    }
    return 0;

}
时间: 2024-10-10 15:40:04

uva 10474 Where is the Marble?[ vector ]的相关文章

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?(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 - 501 Black Box (优先队列或vector)

Description  Black Box  Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empty and i equals 0. This Black Box processes a sequence of commands (transactions).

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

A - Where is the Marble? UVA - 10474

ACM紫书 第五章 P108 [排序与检索] 题意:找输入的数在排完序之后的位置. 想自己用vector写下,却报错 iterator cannot convert '__gnu_cxx::__normal<int*, std::vector<int> >' to 'int' in assignment ·迭代器的接口几乎相当于普通的指针.让一个迭代器递增只需调用++操作符. 使用*操作符可以得到迭代器引用的数据值.因而迭代器可以被任为是一种智能指针 lower_bound ()返

D - Where is the Marble? (UVA - 10474)

- 题目大意 题目中给出一个n个数的序列和q次查询,每次询问查询值是否在序列内,如果在,输出序列升序排列后的位置(从1开始). - 解题思路 先使用sort()函数进行升序排列,然后枚举出其中与查找的值相同的数,符合条件就输出. - 代码 #include<iostream> #include<algorithm> using namespace std; int num[10000]; int main() { int N, Q,q; int a=1,b=-1; while (c