博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
M--二分查找
阅读量:3948 次
发布时间:2019-05-24

本文共 576 字,大约阅读时间需要 1 分钟。

M–二分查找

Time Limit: 600 ms Memory Limit: 65536 KiB

Problem Description

给出含有n个数的升序序列,保证序列中的数两两不相等,这n个数编号从1 到n。
然后给出q次询问,每次询问给出一个数x,若x存在于此序列中,则输出其编号,否则输出-1。
Input
单组输入。首先输入一个整数n(1 <= n && n <= 3000000),接下的一行包含n个数。
再接下来的一行包含一个正整数q(1 <= q && q <= 10000),表示有q次询问。
再接下来的q行,每行包含一个正整数x。
Output
对于每次询问,输出一个整数代表答案。
Sample Input
5
1 3 5 7 9
3
1
5
8
Sample Output
1
3
-1

代码如下:

#include 
#include
int a[3000001];int search(int a[],int l,int r,int k){ if(l<=r) { int mid=(l+r)/2; if(a[mid]==k) { return mid; } else if(a[mid]

转载地址:http://ozhwi.baihongyu.com/

你可能感兴趣的文章
Sending the User to Another App
查看>>
Getting a Result from an Activity
查看>>
Allowing Other Apps to Start Your Activity
查看>>
Using the Location Manager
查看>>
Obtaining the Current Location
查看>>
Displaying the Location Address
查看>>
Connecting to the Network
查看>>
Managing Network Usage
查看>>
Parsing XML Data
查看>>
Optimizing Downloads for Efficient Network Access
查看>>
Minimizing the Effect of Regular Updates
查看>>
Redundant Downloads are Redundant
查看>>
Modifying your Download Patterns Based on the Connectivity Type
查看>>
Supporting Different Screen Sizes支持不同的屏幕尺寸
查看>>
Supporting Different Densities 支持各种屏幕密度
查看>>
Implementing Adaptative UI Flows 实施自适应用户界面流程
查看>>
Crossfading Two Views 淡入淡出的两种观点
查看>>
Using ViewPager for Screen Slides 使用屏幕幻灯片ViewPager
查看>>
Displaying Card Flip Animations 显示卡片翻转动画
查看>>
Zooming a View 缩放视图
查看>>