其他分享
首页 > 其他分享> > 二分及离散化板子

二分及离散化板子

作者:互联网

点击查看代码
#include<cstdio>
#include<algorithm>
using namespace std;
const int M = 1000;
int n,a[100],m;
void discrete(int a[]){
sort(a+1,a+1+n);
a[0] = unique(a+1,a+1+n)-a-1;
}
int query(int x){
return lower_bound(a+1,a+1+a[0],x)-a;
}
int find(int x){
int l = 1,r = n;
while(l<r){
int mid = (l+r+1)>>1;
if(a[mid]>x)l = mid;
else r = mid-1;
}//>=x的最小的,raise
return a[l];
}
int find2(int x){
int l = 1,r = n;
while(l<r){
int mid = (l+r)>>1;
if(a[mid]<x)r = mid;
else l = mid+1;
}
return a[l];
//<=x最大的,raise
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i){scanf("%d",&a[i]);}
printf("%d %d\n",find(6),find2(6));
return 0;
}
/*
10
1 2 3 4 5 6 7 8 9 10
10
10 9 8 7 6 5 4 3 2 1
*/

标签:二分,return,int,mid,板子,离散,while,include,bound
来源: https://www.cnblogs.com/zasdcn/p/15872788.html