UVA.10474 Where is the Marble ( 排序 二分查找 )

题意分析

大水题一道。排序好找到第一个目标数字的位置,返回其下标即可。暴力可过,强行写了一发BS,发现错误百出。应了那句话:基础不牢,地动山摇。

记录一下自己BS的常见错误:

1.需要传入的参数是,搜索的区间[l,r]和搜索的目标值t;

2.一般被搜索的对象以全局变量的身份出现,故不需要传参进去;

3.退出循环的条件是l < r 注意这里可没有等号;

4.若t在mid左边或等于mid,要把右坐标r移动到m的位置,反之,要把l移动到mid+1的位置;

5.最后直接返回l即可,l即为找到的匹配位置,或者是最接近的位置。

6.这种写法还有改进的余地。

代码总览

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define nmax 10005
using namespace std;
int a[nmax];
bool cmp(int a, int b)
{
return a<b;
}
int bs(int l, int r,int t)
{
int m;
while(l<r){
m = (l+r)/2;
if(t<=a[m]) r = m;
else if(t>a[m])l = m+1;
}
return l;
}
int main()
{
//reopen("in.txt","r",stdin);
int n,m;
int cas = 1;
while(scanf("%d%d",&n,&m) &&n){
printf("CASE# %d:\n",cas++);
for(int i =1; i<=n; ++i) scanf("%d",&a[i]);
sort(a+1,a+1+n,cmp);
for(int i = 1;i<=m;++i){
int t;
scanf("%d",&t);
int ans =bs(1,n,t);
if(a[ans] == t) printf("%d found at %d\n",t,ans);
else printf("%d not found\n",t);
//printf("%d\n",bs(1,n,t,f));
}
}
return 0;
}

UVA.10474 Where is the Marble ( 排序 二分查找 )的更多相关文章

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

    我非常奇怪为什么要把它归类到回溯上,明明就是简单排序,查找就OK了.wa了两次,我还非常不解的怀疑了为什么会 wa,原来是我居然把要找的数字也排序了,当时仅仅是想着能快一点查找.所以就给他排序了,没考 ...

  2. 常见的排序算法(直接插入&选择排序&二分查找排序)

    1.直接插入排序算法 源码: package com.DiYiZhang;/* 插入排序算法 * 如下进行的是插入,排序算法*/ public class InsertionSort {    pub ...

  3. I Count Two Three(打表+排序+二分查找)

    I Count Two Three 二分查找用lower_bound 这道题用cin,cout会超时... AC代码: /* */ # include <iostream> # inclu ...

  4. uva 10474 Where is the Marble? 计数排序

    题目给出一系列数字,然后问哪个数字是从小到大排在第几的,重复出现算第一个. 数据范围为10000,不大,完全可以暴力,sort不会超时. 但是由于以前做比赛时也遇到这种题目,没注意看数据范围,然后暴力 ...

  5. UVa 10474 Where is the Marble

    题意:给出一列数,先排序,再查找学习了sort函数,lower_bound函数sort:可以给任意对象排序(包括自己定义的)(前提是定义好了‘<’运算符)lower_bound:查找大于或者等于 ...

  6. UVA 10474 - Where is the Marble?--vector

    https://vjudge.net/problem/UVA-10474 https://blog.csdn.net/xiyaozhe/article/details/81081344 简单用法 so ...

  7. Where is the Marble UVA - 10474

     Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on th ...

  8. UVa10474 Where is the Marble?(排序sort)

    今天开始学STL,这是书上的一道例题,主要是用了sort函数和lower_bound函数,挺容易理解的. lower_bound的作用是查找“大于或等于x的第一个位置”. 需要注意的是,不要忘记alg ...

  9. 10474 - Where is the Marble?(模拟)

    传送门: UVa10474 - Where is the Marble? Raju and Meena love to play with Marbles. They have got a lot o ...

随机推荐

  1. mac使用brew或者tomcat启动jenkins后配置文件路径

    在mac下使用brew命令或tomcat安装jenkins,启动后要输入密码,密码不知道,又找不到config.xml,找了半天原来 config.xml在/Users/qiaojiafei/.jen ...

  2. Prism MEF example

    Related Attributes These attributes are under namespace System.ComponentModel.Composition Import The ...

  3. MaxScript代码补全插件

    MaxScript代码补全插件 作者Nik,原文发布于ScriptSpot 安装后max自带脚本编辑器会有自动补全,效果如下:

  4. 了解Python控制流语句——continue 语句

    continue 语句用以告诉 Python 跳过当前循环块中的剩余语句,并继续该循环的下一次迭代. 案例(保存为 continue.py): while True: s = input('Enter ...

  5. Oracle作业练习题

    第一问 //登陆scott用户 //解锁 alter user scott account unlock; //给用户申请密码 alter user scott identified by tiger ...

  6. 爬虫1.5-ajax数据爬取

    目录 爬虫-ajax数据爬取 1. ajax数据 2. selenium+chromedriver知识准备 3. selenium+chromedriver实战拉勾网爬虫代码 爬虫-ajax数据爬取 ...

  7. java字符转义

    之前对java字符转义这一块稍作了解,在这里理理自己主观浅显的理解 这里会谈谈字符编码的是另一种问题和转义没有关系 以下面代码做分析 System.out.println("a". ...

  8. Python3 数据类型-字典

    字典是一种可变数据类型,且可存储任意类型对象. 字典使用大括号"{}"括起来,由键(key)和值(values)组成,键只能使用不可变类型定义,值可以使用可变类型{'键':'值'} ...

  9. java安装环境变量设置

    1,依次打开:我的电脑-->属性-->高级-->环境变量 2,设置用户变量 新建 JAVA_HOME C:\Program Files\Java\j2sdk1.5.0 (JDK的安装 ...

  10. vue学习笔记(三):vue-cli脚手架搭建

    一:安装vue-cli脚手架: 1:为了确保你的node版本在4.*以上,输入 node -v 查看本机node版本,低于4请更新. 2:输入:  npm install -g vue-cli     ...