二分排序模板
在一个升序序列中,求一个数第一次出现的位置、最后一次出现的位置,不存在返回-1
int first_pos(){
int l = 0;
int r = n - 1;
while(l < r){
int mid = (l + r) / 2;
if(a[mid] < x){
l = mid + 1;
}else{
r = mid;
}
}
if(a[l] != x) return -1;
else return l;
}
int last_pos(){
int l = 0;
int r = n - 1;
while(l < r){
int mid = (l + r + 1) / 2;
if(a[mid] <= x){
l = mid;
}else{
r = mid - 1;
}
}
if(a[l] != x) return -1;
else return l;
}
1 数的范围
题目链接
https://www.acwing.com/problem/content/description/791/
题目大意
给定一个按照升序排列的长度为n的整数数组,以及 q 个查询。
对于每个查询,返回一个元素k的起始位置和终止位置(位置从0开始计数)。
如果数组中不存在该元素,则返回“-1 -1”。
思路一 lower_bound、upper_bound
在升序序列中:
- lower_bound( begin,end,num)从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标
- upper_bound( begin,end,num):第一个大于num的数字
在降序序列中,相反
#include<bits/stdc++.h>
using namespace std;
int a[100005];
// 没找到,xxx_bound(a,a+n,x) - a,返回n
int main(){
int n,q;
cin >> n >> q;
for(int i = 0;i < n;i++){
scanf("%d",&a[i]);
}
while(q--){
int x;
scanf("%d",&x);
int z = lower_bound(a,a+n,x) - a;
if(z == n || a[z] != x){
//没有找到第一个大于或等于x的数,返回n
//由于最后一位可能刚好是x(此时返回n - 1),所以特判
cout << -1 << " " << -1 << endl;
}else{
int i = lower_bound(a,a+n,x) - a;
int j = upper_bound(a,a+n,x) - a;
if(j == n && a[j - 1] != x){
//没有找到第一个大于x的数,返回n
//由于最后一位也有可能是x(此时返回n),所以特判
cout << i << " " << i << endl;
}else{
cout << i << " " << j - 1 << endl;
}
}
}
return 0;
}
思路二 统计一下每个数字的出现位置
#include<stdio.h>
int a[100005];//source
int b[100005];//num_start
int c[100005];//num_end
int d[100005];
int main(){
int m,n;
scanf("%d %d",&m,&n);
int x;
scanf("%d",&x);
a[0] = x;
d[x] = 1;
b[0] = 0;
c[0] = 0;
for(int i = 1;i < m;i++){
scanf("%d",&x);
a[i] = x;
d[x] = 1;
if(x == a[i-1]){
c[x] = i;//update end
}else{
b[x] = i;//update begin
}
}
int p;
for(int i = 0;i < n;i++){
scanf("%d",&p);
if(d[p] != 0){
if(c[p] == 0){
printf("%d %d\n",b[p],b[p]);
}else{
printf("%d %d\n",b[p],c[p]);
}
}else{
printf("-1 -1\n");
}
}
return 0;
}
思路三 二分排序(模板)
详细推导可以看这里
https://www.acwing.com/solution/content/3338/
#include<stdio.h>
#include<iostream>
using namespace std;
const int N = 1e5+5;
int a[N];
int n,q,x;
int first_pos(){
int l = 0;
int r = n - 1;
while(l < r){
int mid = (l + r) / 2;
if(a[mid] < x){
l = mid + 1;
}else{
r = mid;
}
}
if(a[l] != x) return -1;
else return l;
}
int last_pos(){
int l = 0;
int r = n - 1;
while(l < r){
int mid = (l + r + 1) / 2;
if(a[mid] <= x){
l = mid;
}else{
r = mid - 1;
}
}
if(a[l] != x) return -1;
else return l;
}
int main(){
cin >> n >> q;
for(int i = 0;i < n;i++) scanf("%d",&a[i]);
while(q--){
cin >> x;
int f1 = first_pos();
int f2 = last_pos();
cout << f1 << " " << f2 << endl;
}
return 0;
}
2 数的三次方根
题目链接
https://www.acwing.com/problem/content/description/792/
题目大意
给定一个浮点数n,求它的三次方根。
注意,结果保留6位小数。
−10000≤n≤10000
解题思路
二分
#include<bits/stdc++.h>
using namespace std;
double q(double x){
return x * x * x;
}
int main(){
double n;
cin >> n;
double l = -10000;
double r = 10000;
while(r - l >= 1e-7){
double mid = (r + l) / 2;
if(q(mid) >= n){
r = mid;
}else{
l = mid;
}
}
printf("%.6f\n",l);
return 0;
}