二分模板
① 普通的整数二分
int l, r, res;
while(l <= r){
int mid = (l + r) / 2;
if (ok (mid)){
res = mid;
r = mid - 1;
}
else{
l = mid + 1; // 满足条件的最小值
}
}
精度精确型二分
const double eps = 1e-7;
double l, r;
while (l + eps < r){
double mid = (l + r) / 2.0;
if (ok (mid)){
l = mid;
}
else {
r = mid;
}
}
HDU4151 http://acm.hdu.edu.cn/showproblem.php?pid=4151
先是枚举,然后是二分^...^
1 #include<stdio.h> 2 #include<iostream> 3 #include<algorithm> 4 #include<string.h> 5 using namespace std; 6 bool judge[10]; 7 int m,n; 8 bool check(int x){ 9 memset(judge,false,sizeof(judge)); 10 bool flag=true; 11 while(x){ 12 int temp=x%10; 13 x/=10; 14 if(judge[temp]){ 15 flag=false; 16 break; 17 } 18 judge[temp]=true; 19 20 }return flag; 21 } 22 int arr[100000000]; 23 int main(){ 24 int tot=0; 25 for(int i=1;i<10000000;++i){ 26 if(check(i))arr[tot++]=i; 27 } 28 29 while(cin>>n){ 30 int r=tot-1;int l=0; 31 int mid,res; 32 if(n<=1){ 33 cout<<"0"<<endl; 34 continue; 35 } 36 while(l<=r){ 37 mid=(l+r)/2; 38 if(arr[mid]<n) 39 {l=mid+1; 40 res=mid; 41 } 42 else r=mid-1; 43 } 44 int s=res+1; 45 cout<<s<<endl; 46 } 47 }
时间: 2024-10-10 01:52:34