码迷,mamicode.com
首页 > Windows程序 > 详细

ACwing(基础)---789. 数的范围

时间:2020-07-02 13:28:58      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:problem   algorithm   algo   space   href   for   else   ace   max   

789. 数的范围

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

const int maxn =1e5+10;

int a[maxn];

int main() {
	int n,m,x;
	cin>>n>>m;
	for(int i=0;i<n;i++) cin>>a[i];
	while(m--){
		cin>>x;
		int l=0,r=n-1;
		while(l<r){
			int mid=(l+r)>>1;
			if(a[mid]>=x) r=mid;//偏左半边
			else l=mid+1;
		}
		if(a[l]!=x) cout<<"-1 -1\n";
		else{
			cout<< l <<" ";
			int l=0,r=n-1;
			while(l<r){
				int mid=(l+r+1)>>1;
				if(a[mid]<=x) l=mid;//偏右半边
				else r=mid-1;
			}
			cout<< l <<"\n";
		}
	}
	return 0;
}

ACwing(基础)---789. 数的范围

标签:problem   algorithm   algo   space   href   for   else   ace   max   

原文地址:https://www.cnblogs.com/bingers/p/13223898.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!