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 beginning, Raju would place the marbles one after another in ascending order of the numbers written
on them. Then Meena would ask Raju to find the first marble with a certain number. She would count 1...2...3. Raju gets one point for correct answer, and Meena gets the point if Raju fails. After some fixed number of trials the game ends and the player with
maximum points wins. Today it‘s your chance to play as Raju. Being the smart kid, you‘d be taking the favor of a computer. But don‘t underestimate Meena, she had written a program to keep track how much time you‘re taking to give all the answers. So now you
have to write a program, which will help you in your role as Raju.

Input

There can be multiple test cases. Total no of test cases is less than 65. Each test case consists begins with 2 integers: N the number of marbles and Q the
number of queries Mina would make. The next N lines would contain the numbers written on the N marbles. These marble numbers will not come in any particular order. Following Q lines
will have Q queries. Be assured, none of the input numbers are greater than 10000 and none of them are negative.

Input is terminated by a test case where N = 0 and Q = 0.

Output

For each test case output the serial number of the case.

For each of the queries, print one line of output. The format of this line will depend upon whether or not the query number is written upon any of the marbles. The two different formats are described below:

  • `x found at y‘, if the first marble with number x was found at position y. Positions are numbered 1,
    2,..., N.
  • `x not found‘, if the marble with number x is not present.

Look at the output for sample input for details.

Sample Input

4 1
2
3
5
1
5
5 2
1
3
3
3
1
2
3
0 0

Sample Output

CASE# 1:
5 found at 4
CASE# 2:
2 not found
3 found at 3

Problem-setter: Monirul Hasan Tomal, Southeast University

Source

Root :: Prominent Problemsetters :: Monirul Hasan

Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 1. Elementary Problem Solving :: Sorting/Searching

Root :: Competitive Programming: Increasing the Lower Bound of Programming Contests (Steven & Felix Halim) :: Chapter 3. Problem Solving Paradigms :: Divide
and Conquer

Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Problem Solving Paradigms :: Divide
and Conquer -Binary Search

Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 3. Brute Force :: Backtracking - Easy

Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Problem Solving Paradigms :: Divide and Conquer :: Binary
Search

Root :: AOAPC II: Beginning Algorithm Contests (Second Edition) (Rujia Liu) :: Chapter 5. C++ and STL :: Examples

Submit Status

思路:就是给出一组数据,这组数据按从小到大排列,然后再查询一个数是否在数组中,在的话输出排在第几

AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

int num[10005];

int main() {
	int m, n;
	int cas = 1;
	while(scanf("%d %d", &m, &n), m || n) {
		printf("CASE# %d:\n", cas++);
		memset(num, 0, sizeof(num));
		for(int i = 0; i < m; i++) {
			int t;
			scanf("%d", &t);
			num[t]++;
		}
		for(int i = 0; i < n; i++) {
			int t;
			scanf("%d", &t);
			if(num[t] == 0) {
				printf("%d not found\n", t);
			}
			else
			{
				int ans = 0;
				for(int i = 0; i < t; i++) ans += num[i];
				printf("%d found at %d\n", t, ans + 1);
			}
		}
	}
	return 0;
} 
时间: 2024-10-13 00:03:46

UVA - 10474 - Where is the Marble? (基数排序)的相关文章

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

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

我很奇怪为什么要把它归类到回溯上,明明就是简单排序,查找就OK了,wa了两次,我还很不解的怀疑了为什么会 wa,原来是我竟然把要找的数字也排序了,当时只是想着能快一点查找,所以就给他排序了,没考虑到要按给的顺序输 出答案,这次真是二了,,,看别人题解有用打表做的,那个应该是正确解法,我的耗时980ms,估计数据再大一些就 要TLE了 贴代码: #include<stdio.h> #include<string.h> #include<stdlib.h> int cmp(

uva 10474 Where is the Marble?[ vector ]

思路:sort+low_bound()二分 #include<vector> #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<set> using namespace std; int main() { vector<int>v; int n,m; int cas=1; while(scanf("

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

Where is the Marble? Descriptions: 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 th

D - Where is the Marble? (UVA - 10474)

- 题目大意 题目中给出一个n个数的序列和q次查询,每次询问查询值是否在序列内,如果在,输出序列升序排列后的位置(从1开始). - 解题思路 先使用sort()函数进行升序排列,然后枚举出其中与查找的值相同的数,符合条件就输出. - 代码 #include<iostream> #include<algorithm> using namespace std; int num[10000]; int main() { int N, Q,q; int a=1,b=-1; while (c

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

题目描述:算法竞赛入门经典例题5-1 1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 int maxn = 10000 ; 5 int main() 6 { 7 int n,q,a[maxn] ,k=0; 8 while(scanf("%d%d",&n,&q)==2 && n &&q){ 9 for(int i=0;i<

A - Where is the Marble? UVA - 10474

ACM紫书 第五章 P108 [排序与检索] 题意:找输入的数在排完序之后的位置. 想自己用vector写下,却报错 iterator cannot convert '__gnu_cxx::__normal<int*, std::vector<int> >' to 'int' in assignment ·迭代器的接口几乎相当于普通的指针.让一个迭代器递增只需调用++操作符. 使用*操作符可以得到迭代器引用的数据值.因而迭代器可以被任为是一种智能指针 lower_bound ()返