现有N个大理石,每个大理石上写了一个非负整数。首先把各数从小到大排序,然后回 答Q个问题。每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上 写着x。排序后的大理石从左到右编号为1~N。(在样例中,为了节约篇幅,所有大理石上 的数合并到一行,所有问题也合并到一行。)

样例输入:

4 1

2 3 5 1

5 2

1 3 3 3 1

2 3

样例输出:

CASE #1:

5 found at 4

CASE #2:

2 not found

3 found at 3

【分析】

题目意思已经很清楚了:先排序,再查找。使用algorithm头文件中的sort和lower_bound 很容易完成这两项操作,代码如下:

#include<algorithm>
using namespace std;
const int maxn = ;
int main() {
int n, q, x, a[maxn], kase = ;
while(scanf("%d%d", &n, &q) == && n) {
printf("CASE# %d:\n", ++kase);
for(int i = ; i < n; i++) scanf("%d", &a[i]);
sort(a, a+n); //排序
while(q--) {
scanf("%d", &x);
int p = lower_bound(a, a+n, x) - a; //在已排序数组a中寻找x
if(a[p] == x) printf("%d found at %d\n", x, p+);
else printf("%d not found\n", x);
}
}
return ;
}

lower_bound 函数:

 lower_bound()返回值是一个迭代器,返回指向比key大的第一个值的位置。例如:

#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int a[]={,,,,,,,};
printf("%d",lower_bound(a,a+,)-a);
return ;
}

lower_bound函数返回的是一个地址,-a之后变成下标。

不用lower_bound函数:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std; int main() {
int n,m,count=;
while(){ cin>>n>>m;
if(n==) break;
int a[n];
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
printf("CASE# %d:\n",++count);
sort(a+,a+n+);
while(m--){
int x;
scanf("%d",&x);
int flag=;
for(int i=;i<=n;i++){
if(x==a[i]){
printf("%d found at %d\n",x,i);
flag=;
break;
}
}
if(!flag) printf("%d not found\n",x);
}
} return ;
}

大理石在哪儿(Where is the Marble?,Uva 10474)的更多相关文章

  1. 大理石在哪儿 (Where is the Marble?,UVa 10474)

    题目描述:算法竞赛入门经典例题5-1 #include <iostream> #include <algorithm> using namespace std; ; int m ...

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

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

    UVA.10474 Where is the Marble ( 排序 二分查找 ) 题意分析 大水题一道.排序好找到第一个目标数字的位置,返回其下标即可.暴力可过,强行写了一发BS,发现错误百出.应了 ...

  4. 大理石在哪?(Where is the Marble?,UVa 10474)

    参考:ACM紫书 第五章 P108 [排序与检索] 下面的代码中有些 提示性输出,想Ac 需删除提示性输出语句,读者自行修改. #include <cstdio> #include < ...

  5. 【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 ...

  6. 大理石在哪里UVa 10474

    我自己写的代码 #include<iostream>#include<algorithm>using namespace std;int main(){    int N,a[ ...

  7. UVA 10474 大理石在哪 lower_bound

    题意:找输入的数在排完序之后的位置. 主要是lower_bound 函数的使用.它的作用是查找大于或者等于x的第一个位置. #include<cstdio> #include<alg ...

  8. UVa 10474 Where is the Marble

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

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

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

随机推荐

  1. java中间变量缓存机制

    public class Demo { public static void main(String[] args){ method_1(); method_2(); } private static ...

  2. PHP MySQL mysql.sock的问题

    SQLSTATE[HY000] [2002] No such file or directory 原因是找不到mysql.sock这个文件..一般出现的症状就是能用ip连接mysql.但不能使用loc ...

  3. leetcode 690. Employee Importance——本质上就是tree的DFS和BFS

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  4. Java 并发 —— Thread、Executor、线程池

    Java 线程池: ThreadPoolExecutor,创建此线程池的方法: new Executors.newCachedThreadPool():尽量避免使用,其无法控制线程数量, Schedu ...

  5. linux线程相关函数接口

    以下内容转自网络 索引:1.创建线程pthread_create2.等待线程结束pthread_join3.分离线程pthread_detach4.创建线程键pthread_key_create5.删 ...

  6. json返序列化

    ASP.NET中JSON的序列化和反序列化 http://www.cnblogs.com/zhaozhan/archive/2011/01/09/1931340.html 迟来的Json反序列化 ht ...

  7. JVM系列-类加载机制

    简介 在java中,类的声明周期总共分为以下几种: 加载(Loading),验证(Verification),准备(Preparation),解析(Analysis), 初始化(Initializat ...

  8. C++中虚析构函数的作用 (转载)

    转自:http://blog.csdn.net/starlee/article/details/619827 我们知道,用C++开发的时候,用来做基类的类的析构函数一般都是虚函数.可是,为什么要这样做 ...

  9. P2476 [SCOI2008]着色方案

    传送门 数学太珂怕了--膜一下->这里 记\(sum[i]\)为题中\(c[i]\)的前缀和,\(C[i][j]\)表示\(C_{i}^j\) 设\(f[i][j]\)表示前面\(i\)中颜色已 ...

  10. JAVA的双色球 小程序

    还是挺简单的,功能过于强大. import java.util.Arrays; import java.util.Random; import java.util.Scanner; public cl ...