10474 - Where is the Marble? 有技巧地快速查找

Python刷OJ———UVa :10474 Where is the Marble? 首先说一下,本人没有投身ACM的想法,刷题完全是当练习,有些题能accept,其余基本都是LTE。。讲真的,全网太难找到用python实现的代码了,而我就是如此倔强,于是放一些题的代码在博客上,纯属爱好,大家不要当真哈哈哈哈。 ———————————————————————————————————————————— 题目: Raju and Meena love to play with Marb... 阅读详情

原题

提供三个代码。效率由高到低。

当我认为二分的效率已经够高时,我看到了他的文章,又把效率提高了。链接

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
const int maxn=20000;

int be[maxn],pos[maxn];

int main()
{
#ifndef ONLINE_JUDGE
	freopen("10474.txt","r",stdin);
#endif
	int n,q,k=0;
	while(scanf("%d%d",&n,&q) && (n ||q))
	{
		printf("CASE# %d:\n",++k);	
		int i,j=0;
		memset(be,0,sizeof(be));
		for(i=0;i<n;i++) 
		{
			scanf("%d",&j);
			be[j]++;
		}
		j=0;
		for(i=0;i<10010;i++) if(be[i])
		{
			pos[i]=j+1;
			j+=be[i];
		}
		for(i=0;i<q;i++)
		{
			scanf("%d",&j);
			if(be[j])  printf("%d found at %d\n",j,pos[j]);
			else printf("%d not found\n",j);
		}
	}
	return 0;
}

#include<stdio.h>
#include<stdlib.h>
const int maxn=20000;

int A[maxn];

int cmp(const void *_a,const void *_b)
{
	int *a=(int *)_a;
	int *b=(int *)_b;
	return *a-*b;
}

int main()
{
	freopen("10474.txt","r",stdin);
	int n,q,k=0;
	while(scanf("%d%d",&n,&q) && (n ||q))
	{
		printf("CASE# %d:\n",++k);	
		int i;
		for(i=0;i<n;i++) scanf("%d",&A[i]);
		qsort(A,n,sizeof(A[0]),cmp);
		for(i=0;i<q;i++)
		{
			int t;
			scanf("%d",&t);
			int x=0,y=n-1;
			while(x<y)
			{
				int m=x+(y-x)/2;
				if(A[m]>=t)  y=m;
				else x=m+1;
			}
			if(A[x]==t) printf("%d found at %d\n",t,x+1);
			else printf("%d not found\n",t);
		}
		//for(i=0;i<n;i++) printf("%d\n",A[i]);
		//printf("\n");
	}
	return 0;
}

#include<stdio.h>
#include<stdlib.h>
const int maxn=20000;

int A[maxn];

int cmp(const void *_a,const void *_b)
{
	int *a=(int *)_a;
	int *b=(int *)_b;
	return *a-*b;
}

int main()
{
	freopen("10474.txt","r",stdin);
	int n,q,k=0;
	while(scanf("%d%d",&n,&q) && (n ||q))
	{
		printf("CASE# %d:\n",++k);	
		int i;
		for(i=0;i<n;i++) scanf("%d",&A[i]);
		qsort(A,n,sizeof(A[0]),cmp);
		for(i=0;i<q;i++)
		{
			int t;
			scanf("%d",&t);
			int *f=(int *)bsearch(&t,A,n,sizeof(A[0]),cmp);
			if(f==NULL) printf("%d not found\n",t);
			else 
			{	
				while(f!=0 && *(f-1)==*f ) f--;
				printf("%d found at %d\n",t,f-A+1);
			}
		}
		//for(i=0;i<n;i++) printf("%d\n",A[i]);
		//printf("\n");
	}
	return 0;
}


Ubuntu开发环境配置——持续更新 本文简要介绍了Ubuntu下的开发环境配置流程和问题分析与解决,更多内容持续更新中。 阅读详情

相关推荐

R语言分类变量的统计描述

三篇介绍了连续变量的统计描述的R语言相关函数,计算数值的相关统计指标。本文介绍分类变量的统计描述及如何在R语言中实现。 不同于连续变量,分类变量的统计描述主要是汇总分类变量各类别例数及百分比。 1、table()函数 ...

zsc943290710的博客 1万+

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 afte

总有那么一群人喜欢盯着我的博客看,估计这是他们见过最长的博客名,我必须得告诉他们博客名最长可以写多长 732

从设计到实战:手把手教你用SolidWorks完成一个简易离心泵叶轮的水力设计全流程

本文详细介绍了使用SolidWorks进行简易离心泵叶轮水力设计的全流程,涵盖参数计算、水力设计和三维建模等关键步骤。通过实际案例演示,帮助机械设计工程师和学生掌握从理论到实践的转化技巧,特别适合低比转速离心泵叶轮的设计需求。

weixin_30621711的博客 248

【每日一题(17)】Where is the Marble? UVA - 10474

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 them. At the beginning, Raju would place the marbles one after

Winsoul Blog 1万+

CanOpen PDO映射实战:从原理到配置的完整指南

本文详细解析了CanOpen PDO映射的原理与配置流程,从基础通信机制到实战配置步骤,涵盖禁用PDO、清空映射、逐项映射和使能PDO等关键环节。特别针对西门子G120变频器、台达伺服和倍福CX控制器等不同厂商设备,提供了具体的配置技巧和常见问题排查指南,帮助工程师快速掌握多厂商设备间的PDO通信配置。

weixin_30718391的博客 527

工程伦理ppt

工程伦理学 第一章 工程与伦理 1.1 如何理解工程 一、技术与工程的区别 二、技术与工程的联系 三、工程的定义 五、工程具有不确定性和探索性 六、理解工程活动的7个维度 1.2 如何理解伦理 一、道德与伦理 二、不同的伦理立场 三、伦理困境与伦理选择 1.3 工程实践中的伦理问题 一、工程活动中的行动者网络(具有动态性和网络性) 二、主要的工程伦理问题 三、工程伦理问题的特点 1.4 如何处理工程实践中的伦理问题 一、工程实践中伦理问题的辨识 二、处理工程伦理问题的基本原则 三、应对工程伦理问题的基本思路

weixin_51656605的博客 7万+

uva 10474 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 ano

不慌不忙、不急不躁 1410

YOLO详解

本文参考知乎 https://zhuanlan.zhihu.com/p/25236464 1. YOLO的创新点 YOLO将物体检测作为回归问题求解。基于一个单独的end-to-end网络,完成从原始图像的输入到物体位置和类别的输出。 从网络设计上,YOLO与two steps的目标检测方法如RCNN、Fast RCNN和Faster RCNN的区别如下: YOLO训练和检测均是在一个单独网...

UnknownPlayer的博客 1万+

UVA 10474 Where is the Marble? (STL最简单运用)

Where is the Marble?   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 a

AC_Dreameng 5432

【SpringBoot 4.x 第2节】夯爆了,学习 Spring Boot 4.x 前,需要具备哪些 Java 基础?

🏆 本文收录于 《滚雪球学 Spring Boot 4.x》 专栏。 本专栏面向 有一点 Java 基础,但没有系统学过 Spring Boot 的读者,采用“滚雪球式学习法”:先跑通、再理解、再重构、再上线,最终带你从第一个 Hello API 一路完成一个可部署、可监控、可扩展的后端项目。

**My Coding Family** 4608

用 SQL Trace 透视 SAP HANA 执行链路:从会话层到内部语句,一次把性能问题抓个现行

摘要:SAP HANA 的 SQL Trace 是性能分析和故障定位的关键工具,它记录会话层所有执行的 SQL 语句。文章解释了 SQL Trace 的工作原理、使用场景和配置方法,包括图形界面和 SQL 命令两种启用方式。重点讨论了存储过程内部语句的捕获问题,指出需要通过设置 internal 参数才能获取过程体中的详细 SQL 语句。文章还提供了实用的排查建议,强调要精准控制 Trace 范围,避免生成过大文件。最后通过真实案例展示了如何利用 SQL Trace 快速定位性能问题。(149字)

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1050

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

小G的ACM之路 823

海思3403烧结、复用及mic输入

3403调试的第一步是版本烧结,版本烧结经常遇到问题,本文记录总结遇到的各种问题。

平面到立体 1365

UVa 10474 - Where is the Marble?解题报告

读入一行2个数字 N 和 Q, N 表示接下来的 N 行数字为 marbles, Q 表示 N 行数字之后的 Q 行数字为 query. 要求把 N 行 marbles 从小到大排序, 然后输出每个 Q 在 marbles 中的位置.注意是第一次出现的位置。 思路:用快速排序对其进行排序,然后用二分查找查找其第一次出现的位置。 可以用其他的排序和搜索算法。如qsort。 #include

Yonah潇的专栏 637

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

10474:Where is the Marble?

Where is the Marble?排序加二分查找#include<cstdio> #include<algorithm> using namespace std; const int maxn = 100000; int n,q,mar[maxn]; int main(){ int kase = 0; whi...

diaosuiwa6230的博客 189

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

【UVA - 10474 】Where is the Marble?(排序)

Where is the Marble? Descriptions: RajuandMeenalove to play with Marbles. They have got a lot of marbles with numbers written on them. At the beginning,Rajuwould place the marbles one after ano...

weixin_30593443的博客 153

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

Yvetteweiss的博客 259

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

gaoxiangnumber1——Gao Xiang's Blog 855

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 ano

TOKHE的专栏 1625

UVA10474 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 number...

海岛Blog 4617

uva-10474 Where is the Marble? 题解

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

caizhixinczx的博客 274

Where is the Marble? UVA - 10474

Where is the Marble? UVA - 10474Raju 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

BackToMe的博客 418

10474 Where is the Marble?

题目大意: 找出一个数在给定的一组数中是第几小,如果不存在就输出notfound 运用计数排序来找到第N小 一开始做的时候一直错因为 cout<<" not found\n"<<endl; 换了两次行结果一直没有发现 Where is the Marble? Raju and Meena love to p...

aiezhen1717的博客 158

UVA 10474 Where is the Marble?

yiyi:用两个数组,一个a,一个b.先把a用qsort(),排序,再在a中找看有没b中的元素,如果有,输出它在a中最小的位置,因为位置要从一开始算,所以输出位置时要加一。 还要注意一下数组的大小,我一开始开的很小,就过不了。 Where is the Marble? Raju and Meena love to play with Marbles. They have got a

哈希的博客 358

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

友谊无价 442

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 number

Ahonn's Acm 300

UVA 10474 Where is the Marble?

题目链接:UVA 10474 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 beg

一蓑烟雨 408
上一篇: uva 196 - Spreadsheet
下一篇: uva 110 - Meta-Loopless Sorts
lzw_java
博客等级 码龄15年 49粉丝 73原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址