大理石在哪儿(Where is the Marble?,Uva 10474)
现有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
【分析】
题目意思已经很清楚了:先排序,再查找。使用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)的更多相关文章
- 大理石在哪儿 (Where is the Marble?,UVa 10474)
题目描述:算法竞赛入门经典例题5-1 #include <iostream> #include <algorithm> using namespace std; ; int m ...
- 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 ...
- UVA.10474 Where is the Marble ( 排序 二分查找 )
UVA.10474 Where is the Marble ( 排序 二分查找 ) 题意分析 大水题一道.排序好找到第一个目标数字的位置,返回其下标即可.暴力可过,强行写了一发BS,发现错误百出.应了 ...
- 大理石在哪?(Where is the Marble?,UVa 10474)
参考:ACM紫书 第五章 P108 [排序与检索] 下面的代码中有些 提示性输出,想Ac 需删除提示性输出语句,读者自行修改. #include <cstdio> #include < ...
- 【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 ...
- 大理石在哪里UVa 10474
我自己写的代码 #include<iostream>#include<algorithm>using namespace std;int main(){ int N,a[ ...
- UVA 10474 大理石在哪 lower_bound
题意:找输入的数在排完序之后的位置. 主要是lower_bound 函数的使用.它的作用是查找大于或者等于x的第一个位置. #include<cstdio> #include<alg ...
- UVa 10474 Where is the Marble
题意:给出一列数,先排序,再查找学习了sort函数,lower_bound函数sort:可以给任意对象排序(包括自己定义的)(前提是定义好了‘<’运算符)lower_bound:查找大于或者等于 ...
- uva 10474 Where is the Marble? 计数排序
题目给出一系列数字,然后问哪个数字是从小到大排在第几的,重复出现算第一个. 数据范围为10000,不大,完全可以暴力,sort不会超时. 但是由于以前做比赛时也遇到这种题目,没注意看数据范围,然后暴力 ...
随机推荐
- 解决Eclipse alt+/不出来提示的问题
1. 检查windows ——preferences ——java ——editor —— content assist - advanced,在右上方有一行“select the proposal ...
- YTU 2634: E3 继承了,成员函数却不可访问
2634: E3 继承了,成员函数却不可访问 时间限制: 1 Sec 内存限制: 128 MB 提交: 521 解决: 435 题目描述 下面的程序中,派生类Derive继承自基类Base,mai ...
- zabbix 监控zookeeper
1.监控脚本如下:check_zookeeper.sh 1 2 #!/bin/bash echo mntr | nc 127.0.0.1 2182 | grep "$1" |awk ...
- 【IOI2013】【Bzoj3246】Dreaming
http://www.lydsy.com/JudgeOnline/problem.php?id=3246 中文题面 天地之初,世界尚在遥远的梦想之中. Serpent(水蛇)生活的地方有N个水坑,编号 ...
- c++ class does not name a type (转载)
转载:http://blog.csdn.net/typename/article/details/7173550 declare class does not name a type 出现这个编译错误 ...
- SQLAlchemy 反向生成 model 模型
前言 Django 反向生成的 model 模型的命令 : python manager.py inspectdb SQLAlchemy / Flask-SQLAlchemy则是: pip3 ins ...
- E20170527-ts
asset n. 资产,财产; 有价值的人或物; 有用的东西; 优点; serializer [词典] 串行(化)器(把并行数据变成串行数据的寄存器); 编程语言中,可被序列化的; inflec ...
- MySQL基础 — 详细安装
MySQL--安装 打开MySQL 5.5 安装文件开始: 点击Next 打上勾,再点击Next 点击Custom,说明如下: Typical(典型安装) Installs the mo ...
- bzoj 1645: [Usaco2007 Open]City Horizon 城市地平线【线段树+hash】
bzoj题面什么鬼啊-- 题目大意:有一个初始值均为0的数列,n次操作,每次将数列(ai,bi-1)这个区间中的数与ci取max,问n次后元素和 离散化,然后建立线段树,每次修改在区间上打max标记即 ...
- bzoj21012101: [Usaco2010 Dec]Treasure Chest 藏宝箱(滚动数组优化dp)
2101: [Usaco2010 Dec]Treasure Chest 藏宝箱 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 592 Solved: ...