D3278. 二分查找1

问题描述

  二分查找又叫折半查找(Binary Search, Half-Interval Search),在有序序列中可以用O(1)时间初始化,O(logN)的时间进行查找。
  给定一个长为N的严格升序序列a(a[0], a[1] ... a[N-1])。有M个询问,每个询问有一个参数x,求x在a中的下标(从0开始),若不存在则为-1。

输入格式

  输入共N+M+1行。
  第一行两个整数N,M。
  接下来N行,每行一个数,表示给定序列。
  接下来

输出格式

  输出共M行
  每行一个数,表示所求下标。

样例输入


3 7
1
3
5
0
1
2
3
4
5
6

样例输出


-1
0
-1
1
-1
2
-1

数据规模和约定

  共10组数据。

TNM
012*10^6
122*10^6
232*10^6
3101.5*10^6
4501.5*10^6
51001.5*10^6
610^310^6
710^410^6
810^510^6
910^610^5


  所有数均为非负整数且在int范围内。

基础代码(c++)

#include <iostream>
using namespace std; 
int main() {
    int N, M;
    cin >> N >> M;
    
    int a[N];
    for (int i = 0; i < N; ++i) {
        cin >> a[i];
    }

    for (int i = 0; i < M; ++i) {
        int x;
        cin >> x;

        int left = 0, right = N - 1;
        int ans = -1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (a[mid] == x) {
                ans = mid;
                break;
            } else if (a[mid] < x) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        cout << ans << endl;
    }

    return 0;
}

 but*但是如果使用清橙网络自动评测系统

那么代码为

#include <cstdio>

int main() {
    int N, M;
    scanf("%d %d", &N, &M);
    
    int a[N];
    for (int i = 0; i < N; ++i) {
        scanf("%d", &a[i]);
    }

    for (int i = 0; i < M; ++i) {
        int x;
        scanf("%d", &x);

        int left = 0, right = N - 1;
        int ans = -1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (a[mid] == x) {
                ans = mid;
                break;
            } else if (a[mid] < x) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        printf("%d\n", ans);
    }

    return 0;
}

请C++选手使用scanf读入,cin会超时。

  scanf用法: int soysauce; scanf("%d", &soysauce);

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A binary search algorithm (or binary chop) is a technique for finding a particular value in a sorted list. It makes progressively better guesses, and closes in on the sought value, by comparing an element halfway with what has been determined to be an element too low in the list and one too high in the list. A binary search finds the median element in a list, compares its value to the one you are searching for, and determines if it’s greater than, less than, or equal to the one you want. A guess that turns out to be too high becomes the new top of the list, and one too low the new bottom of the list. The binary search's next guess is halfway between the new list's top and bottom. Pursuing this strategy iteratively, it narrows the search by a factor 2 each time, and finds your value. A binary search is an example of a divide and conquer algorithm (more specifically a decrease and conquer algorithm) and a dichotomic search (more at Search algorithm). The most common application of binary search is to find a specific value in a sorted list. To cast this in the frame of the guessing game (see Example below), realize that we are now guessing the index, or numbered place, of the value in the list. This is useful because, given the index, other data structures will contain associated information. Suppose a data structure containing the classic collection of name, address, telephone number and so forth has been accumulated, and an array is prepared containing the names, numbered from one to N. A query might be: what is the telephone number for a given name X. To answer this the array would be searched and the index (if any) corresponding to that name determined, whereupon it would be used to report the associated telephone number and so forth. Appropriate provision must be made for the name not being in the list (typically by returning an index value of zero), indeed the question of interest might be only whether X is in the list or not. If the list of names is in sorted order, a binary search will find a given name with far fewer probes than the simple procedure of probing each name in the list, one after the other in a linear search, and the procedure is much simpler than organising a hash table though that would be faster still, typically averaging just over one probe. This applies for a uniform distribution of search items but if it is known that some few items are much more likely to be sought for than the majority then a linear search with the list ordered so that the most popular items are first may do better. The binary search begins by comparing the sought value X to the value in the middle of the list; because the values are sorted, it is clear whether the sought value would belong before or after that middle value, and the search then continues through the correct half in the same way. Only the sign of the difference is inspected: there is no attempt at an interpolation search based on the size of the differences. Your task is to write a program that, given a set numbers of ascending and a key, finding a particular postion in a sorted list.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值