UVa Where is the Marble? (STL)

链接:http://acm.hust.edu.cn/vjudge/problem/19833分析:sort和lower_bound函数的应用。sort可以对任意对象排序,但需要定义“小于”运算符,或在排序时传入一个“小于”函数。普通数组用sort(a, a + n)的方式调用,vector用sort(v.begin(), v.end())的方式调用。排序后可以用lower_bound查找“大于或等于x的第一个位置”。本题将所有大理石按升序排好后,用lower_bound查找大于或等于询问整数在数组中的第一个位置,然后判断x是否存在,存在则输出位置p+1。
 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4
 5 const int maxn = 10000;
 6 int a[maxn];
 7
 8 int main() {
 9     int n, q, kase = 0;
10     while (scanf("%d%d", &n, &q) == 2 && n) {
11         printf("CASE# %d:\n", ++kase);
12         for (int i = 0; i < n; i++) scanf("%d", &a[i]);
13         sort(a, a + n);
14         while (q--) {
15             int x; scanf("%d", &x);
16             int p = lower_bound(a, a + n, x) - a;
17             if (a[p] == x) printf("%d found at %d\n", x, p + 1);
18             else printf("%d not found\n", x);
19         }
20     }
21     return 0;
22 }
				
时间: 2024-10-11 05:21:18

UVa Where is the Marble? (STL)的相关文章

uva 12096 - The SetStack Computer(STL)

UVA 12096 - The SetStack Computer 题目链接 题意:几个操作,push是在栈顶加入一个空集,dup是复制栈顶集合,在放入栈顶,union是把头两个取并集放回,int是头两个取交集放回,add是取头两个,把第一个当成一个集合加入第二个,每次操作输出栈顶集合的里面的个数 思路:用set,stack模拟,然后利用map去hash一个集合,模拟即可 代码: #include <cstdio> #include <cstring> #include <s

UVA - 246 10-20-30 (模拟+STL)

Description  10-20-30  A simple solitaire card game called 10-20-30 uses a standard deck of 52 playing cards in which suit is irrelevant. The value of a face card (king, queen, jack) is 10. The value of an ace is one. The value of each of the other c

UVa 101 - The Blocks Problem STL

题目:给你n个方块,有四种操作: 1.move a onto b,把a和b上面的方块都放回原来位置,然后把a放到b上面: 2.move a over b,把a上面的放回原处,然后把a放在b所在的方块堆的上面: 3.pile a onto b,把b上面的放回原来位置,然后把a和a上面的方块整体放到b上面: 4.pile a over b,把a和a上面的方块整体放到b所在堆的上面. 分析:模拟,数据结构.观察操作,如果是move就是先把a上面的还原,如果是onto就是先把b上面的还原. 然后,就是移

UVA - 540 Team Queue(STL,队列 )

Team Queue Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Queues and Priority Queues are data structures which are known to most computer scientists. The Team Queue, however, is not so well known,

UVa - 12096 The SetStack Computer(STL容器综合,强推!)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42064 #include <iostream> #include <algorithm> #include <string> #include <map> #include <set> #include <vector> #include <stack> #define ALL(x) x.b

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 12657 - Boxes in a Line(AC和TLE的区别,为什么说STL慢..)

用STL中的list写的,TLE #include<cstdio> #include<iostream> #include<cstring> #include<list> #include<algorithm> using namespace std; list<int> l; list<int>::iterator it1,it2,it3,it4,it5,it; void work(int a,int a1=1,int

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.