Range of Numbers

该文章介绍了一个在有序整数数组中寻找特定元素范围的算法。给定一个升序排列的数组和多个查询,每个查询要求找到数组中等于指定值的元素的起始和结束位置。解决方案利用了二分查找方法,首先找到第一个和最后一个匹配元素的位置,然后返回这些位置作为结果。如果数组中不存在目标值,则返回-1-1。代码示例展示了如何实现这个功能。
摘要由CSDN通过智能技术生成

range of numbers

Given an integer array of length n sorted in ascending order, and q queries.
For each query, return the start and end positions of an element k (positions are counted from 0). Returns -1 -1 if the element does not exist in the array.

input format
The first line contains integers n and q, indicating the length of the array and the number of queries.
The second line contains n integers (all in the range 1∼10000), representing the complete array. Next q lines, each line contains an integer k, representing a query element.

output format
There are q lines in total, and each line contains two integers, indicating the starting position and ending position of the element to be sought. Returns -1 -1 if the element does not exist in the array.

data range
1≤n≤100000
1≤q≤10000
1≤k≤10000
Input sample:
6 3
1 2 2 3 3 4
3
4
5
Sample output:
3 4
5 5
-1 -1

Solution


This code implements a function rangeOfNumbers that takes an integer array q, the length n of the array, and an integer x as parameters. The goal of this function is to find the subscript range of all elements in the array q that are equal to x and return the result as a string.

This function uses the binary search algorithm to find the index range of elements in the array q that are equal to x. Specifically, the function first uses the binary search algorithm to find the index l of the first element equal to x in the array q, and then uses the binary search algorithm to find the index r of the last element in the array q equal to x. Finally, the function converts the subscript range [l, r] to a string and returns it.

In the first binary search, the function uses the variables l and r to denote the left and right boundaries of the current search range. On each iteration, the function computes the index mid of the middle element and compares it with the target element x. If the middle element is greater than or equal to the target element, update the right bound r to mid, otherwise update the left bound l to mid + 1. The function ends the first binary search when the left bound l is equal to the right bound r.

In the second binary search, the function again uses the variables l and r to denote the left and right boundaries of the current search range. On each iteration, the function computes the index mid of the middle element and compares it with the target element x. If the middle element is less than or equal to the target element, update the left border l to mid, otherwise update the right border r to mid - 1. The function ends the second binary search when the left bound l is equal to the right bound r.

Finally, the function converts the subscript range [l, r] to a string and returns it. If no element equal to x exists in the array q, the function returns the string "-1 -1".

Note that the function assumes that the array q is sorted in ascending order. If the array q is unsorted, the behavior of this function is undefined.

#include <bits/stdc++.h>

using namespace std;

const int N = 100010;

int n, m;
int q[N];

int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i ++ ) cin >> q[i];

    while (m -- ) {
        int x;
        cin >> x;

        int l = 0, r = n - 1;
        while (l < r) {
            int mid = (l + r) >> 1;
            if (q[mid] >= x) r = mid;
            else l = mid + 1;
        }
        if (q[l] != x) cout << "-1 -1" << endl;
        else {
            cout << l << ' ';
            l = 0, r = n - 1;
            while (l < r) {
                int mid = (l + r + 1) >> 1;
                if (q[mid] <= x) l = mid;
                else r = mid - 1;
            }
            cout << l << ' ' << endl;
        }
    }
    return 0;
}

summary

For dichotomy, an intermediate value is found each time as the dividing line, on both sides of the value, one side satisfies a property, and the other side does not satisfy a property. Since this question is to find the range of numbers, and the sequence is increasing, you can use the dichotomy method and pay attention to the boundary conditions. When the sequence is on the right (l=mid), it should be rounded down to avoid an infinite loop

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值