Python刷OJ———UVa :10474 Where is the Marble?

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

首先说一下,本人没有投身ACM的想法,刷题完全是当练习,有些题能accept,其余基本都是LTE。。讲真的,全网太难找到用python实现的代码了,而我就是如此倔强,于是放一些题的代码在博客上,纯属爱好,大家不要当真哈哈哈哈。
————————————————————————————————————————————

题目:

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
————————————————————————————————————————————
大体意思就是先给一个整数 N 和 Q ,接下来将会再给 N 个整数,然后你要把他们从小到大排序。然后再给你 Q 个整数,让你找这 Q 个整数是否在 N 中存在,存在和不存在都有对应的输出,见示例

思路
一个排序,一个查找。排序的话一般快排就可以,既然是有序数列,那肯定是二分法查找
我自己的想法是,对于每一组数据,先把 N 个数和 Q 个数全都接收完(用列表)。然后排序,写一个二分查找的函数。然后用map()直接并行找所有 Q 中的数。最后将返回结果输出即可。

上代码

from sys import stdin, setrecursionlimit

setrecursionlimit(1000000
uva最全ac代码 立即下载

相关推荐

UVA10474 Where is the Marble?

UVA 10474

C++ --- UVa10474. Where is the Marble? 解题报告

本博文源于《算法竞赛入门经典(第二版)》,旨在解决大理石在哪儿(Where is the Marble? Uva10474)这道题目,渐渐走入题库训练。博文目录有:1.问题再现2.样例输入&样例输出3.题目分析4. 代码测试效果5.代码讲解及完整源码

执念斩长河 1376

uva-10474 Where is the Marble? 题解

紫书第五章练习 uva-10474题解;排序函数sort和二分查找函数lower_bound等的使用

caizhixinczx的博客 272

UVA 10474 Where is the Marble?

题目链接:https://vjudge.net/problem/UVA-10474 题意:多组数据,第一行,N,Q   N代表有N个数,Q代表有Q次询问,         接下来是N个数,    然后是Q个询问,每个询问是一个数,代表这个数在原来的N个数中第几(升序) 用计数排序很容易解决了,计数排序思想很简单,当然其他方法也可以,可以排序STL二分,也能过,水题水解。 #include

CS33sun的博客 451

UVa10474(排序和查找)

//思路:用vector存储,在sort排序就可以了 AC代码: #include #include #include using namespace std; int main() { int N,Q,kase=0; while(cin>>N>>Q&&N) { cout<<"CASE# "<<(++kase)<<":"<<endl; vector ivec;

welcome to 浩·C's blog 528

UVA 10474(紫书订正)

https://vjudge.net/problem/UVA-10474 分析:VJ的题,竟然翻车了2次。。。 这道题主要在于二分思想的使用以及STL模板的应用 需要注意的是本题在多组输入方面存在一些坑点:在lower_bound查找中容易出现一些问题,比如上一组输入中存在12,且位置为10,但在下一组中只有5个数,询问中含有12,本来应该输出"12 not found"但如果不加判读就会出现 “12 found at 10”,然而本题中只出现了5个数。。。因此需要判断a[y]==x&&y&

weixin_45655404的博客 219

uvaoj10474---详解细节(二分法)注意!

uvaoj 10474  这题呢,太水了...不过初学者可以练习一下。     题意:输入两组数据,一组数据有m个 一组有n个,找第二个数据每个数在第一个数据中(从小到大的排后)的位置     要求:1.数组足够大10000应该可以了           2.什么排序都可以,不会超时的           3.查找的时候,有人用二分法,其实我不太建议,直接顺序找就可以了,比如1,1,1,

sinat_AQ的博客 712

Uva 10474大理石在哪儿——Vector写法

#include "cstdio" #include "iostream" #include "algorithm" #include "vector" using namespace std; int main(){ int count; int num; int record; int kase = 1; int question; vect

爱学习的小武童鞋 285

UVA 10474-Where is the Marble?(排序查找在数字在新序列中的位置)

Where is the Marble? Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description   Where is the Marble?  Raju and Meena lo

Rocky0429 1187

UVA 10474

#include #include #define maxn 10000 using namespace std; int a[maxn],b[maxn]; int main() { int n,q,k=1; while(cin>>n>>q,n||q) { for(int i=0;i<n;i++) cin>>a[i]; for(int i=0;i<q;i++) cin

努力努力再努力 245

UVA 10474Where is the Marble?

UVA 10474Where is the Marble?题目大意:先输入 2 个数字,一个是被查找的元素个数,一个是需要查找的元素个数,然后输入元素,如果输入 0 则结束,然后将被查找元素排序后查找解题思路:先排序,然后找出元素所在位置#include <stdio.h> #include <iostream> using namespace std; int main() { int

ZeroLH00的博客 377

UVA 10474

#include<iostream> using namespace std; int main() { int n,m,i,temp,flag=1; while(cin>>m>>n) { if(m==0&&n==0)break; cout<<"CASE# "...

weixin_30401605的博客 180

UVA 10474 - Where is the Marble?--vector

https://vjudge.net/problem/UVA-10474 https://blog.csdn.net/xiyaozhe/article/details/81081344 简单用法 sort(start,end) 默认是升序 实现降序: #include<functional> int a[10]={5,6,7,8,9,0,1,2,3,4};vector ...

weixin_30530939的博客 179

UVA - 10474

第五章的第一个大理石的题,附上书上的代码#include #include using namespace std; const int maxn=10000; int main() { int n,q,x,a[maxn],kase=0; while(scanf("%d%d",&n,&q)==2&&n) { printf("CASE# %d:\n",++ka

qq_35692873的博客 326

UVA-10474

题目 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 nu...

得未曾有 心净踊跃 412

uva 10474

#include #include #include #include #include using namespace std; int num[10010]={0}; int cmp(int a,int b) { return a<b; } int main() { int m,n,i,j,k; int t=0; while(scanf("%d%d",&m,&n)==2) { i

classmonster的博客 256

《算法竞赛入门经典》 例题5-1 大理石在哪(Where is the MarbleUVa 10474

原题及翻译 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...

Alex 1014

STL--vector容器find()函数的用法及UVa10474挖的坑

STL

Vapor_的博客 9651

J - Where is the Marble?

J - 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 Meena w

qq_51676888的博客 200
上一篇: 简单易懂,全面介绍机器学习入门(小白必读)
下一篇: 还在苦恼于Python刷OJ不通过?还难以搜到答案?别放弃,这些GitHub上的仓库或许能帮助你(持续更新....)
CxsGhost
博客等级 码龄7年 53粉丝 42原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值