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 f…
UVA.10474 Where is the Marble ( 排序 二分查找 ) 题意分析 大水题一道.排序好找到第一个目标数字的位置,返回其下标即可.暴力可过,强行写了一发BS,发现错误百出.应了那句话:基础不牢,地动山摇. 记录一下自己BS的常见错误: 1.需要传入的参数是,搜索的区间[l,r]和搜索的目标值t; 2.一般被搜索的对象以全局变量的身份出现,故不需要传参进去; 3.退出循环的条件是l < r 注意这里可没有等号; 4.若t在mid左边或等于mid,要把右坐标r移动到m的位置,…
现有N个大理石,每个大理石上写了一个非负整数.首先把各数从小到大排序,然后回 答Q个问题.每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上 写着x.排序后的大理石从左到右编号为1-N.(在样例中,为了节约篇幅,所有大理石上 的数合并到一行,所有问题也合并到一行.) 样例输入: 4 1 2 3 5 1 5 5 2 1 3 3 3 1 2 3 样例输出: CASE #1: 5 found at 4 CASE #2: 2 not found 3 found at 3 [分析]…
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…
题意:给出一列数,先排序,再查找学习了sort函数,lower_bound函数sort:可以给任意对象排序(包括自己定义的)(前提是定义好了‘<’运算符)lower_bound:查找大于或者等于x的第一个位置. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ; int a[maxn]; int main() {…