UVA 10474 STL

题目实在是水题,主要是学习sort以及 lower_bound

x为待查找的元素

int p=lower_bound(a,a+n,x)-p;返回a中第一个大于或等于x的元素的位置,使用lower_bound前要将数组进行排序。函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置,且last的位置是越界的!

upper_bound

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int stone[10010];
int main() {
	int n,q,x;
	int test=0;
	while(~scanf("%d%d",&n,&q)) {
		if(n==0 && q==0) break;
		test++;
		memset(stone,0,sizeof(stone));
		for(int i=0;i<n;i++) {
			scanf("%d",&stone[i]);
		}
		sort(stone,stone+n);
		printf("CASE# %d:\n",test);
		for(int i=0;i<q;i++) {
			scanf("%d",&x);

			int p=lower_bound(stone,stone+n,x)-stone;
			if(stone[p]==x) {
				printf("%d found at %d\n",x,p+1);
			}
			else {
				printf("%d not found\n",x);
			}
		}

	}

	return 0;
}
时间: 2024-10-13 16:06:51

UVA 10474 STL的相关文章

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 - 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?(排序)

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 12096 STL map set 的使用

set这个容器也是STL库的一员,并且在algorithm内直接有 set_union set_intersection  这样求并集交集的操作 map 最方便的地方就是 支持下标访问 举例说明 : 1 #include<iostream> 2 include<cstdio> 3 #include<cstring> 4 #include<vector> 5 #include<map> 6 #include<set> 7 #includ

UVA 10474

题意:给你一组数,再给几个数问是否在一组数中. 题很简单:STL入门. 没用到STL. #include<iostream> #include<cstdio> #include<algorithm> using namespace std; int a[10005]; int main() { int ncase = 1; //freopen("in.txt","r",stdin); int n,m; while(scanf(&q

uva 11995(stl)

题意:1 x,表示放进x元素,2表示拿出一个元素,给出n条指令,然后2 x表示取出的数据是什么,问可以从输入输出判断出是哪种数据结构(栈,队列,优先队列),如果有多种满足,就输出not sure,都不是就输出impossible. 题解:直接定义三个数据结构的stl变量,然后模拟放入数据,到拿出数据时和三种比对判断,可以知道是哪种数据结构. #include <stdio.h> #include <queue> #include <stack> using namesp

UVA 11995 STL 使用

There is a bag-like data structure, supporting two operations: 1 x Throw an element x into the bag. 2 Take out an element from the bag. Given a sequence of operations with return values, you're going to guess the data structure. It is a stack (Last-I

Uva 10474 sort以及lower_bound的用法

现有N个大理石,每个大理石上写了一个非负整数.首先把各数从小到大排序,然后回 答Q个问题.每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上 写着x.排序后的大理石从左到右编号为1-N.(在样例中,为了节约篇幅,所有大理石上 的数合并到一行,所有问题也合并到一行.)样例输入:4 1          (N Q)2 3 5 1    (石头)5             (问题)5 21 3 3 3 12 3样例输出:CASE #1:5 found at 4CASE #2:2 n

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