1、时间复杂度是O(log2n)
2、二分法的适用情况一般满足以下几点:
(1)该数组数据量巨大,需要对处理的时间复杂度进行优化;
(2)该数组已经排序;
(3)一般要求找到的是某一个值或一个位置。
3、具体做法是:先取数组中间位置(mid=(left+right)/2)的数据元素与给定值比较。若相等,则查找成功;否则,若给定值比该数据元素的值小(或大),则给定值必在数组的前半部分[left,mid-1](或后半部分[mid+1,right]),然后在新的查找范围内进行同样的查找。
例题:
问题 C: 山头狙击战
时间限制: 1 Sec 内存限制: 128 MB
[提交] [状态]题目描述
Lucky为了掩护大部队,单枪匹马同敌人周旋,后来被敌人包围在某山头……等等,为什么怎么听怎么像狼牙山五壮士!不过不用着急,这次Lucky携带了足够的弹药,完全可以将涌上来的敌人一个一个干掉。Lucky是个神枪手,只要他的枪膛中有子弹,他就能将在他射程m(用从敌人位置到山头的直线距离算)以内的一个敌人瞬间射杀。但如果在射程内没有敌人,出于节约子弹考虑和面子问题,Lucky会等待敌人靠近然后射击。
正当Lucky为自己的强大而自我膨胀时,他忽然发现了一个致命的失误:他携带的枪是单发枪,每射出一发子弹都必须花k秒钟的时间装子弹。而凶残的敌人才不会花时间等你换子弹呢。他们始终在以1m/s的速度接近山头。而如果在一个敌人到达山头时Lucky无法将他击毙,那么我们可怜的Lucky就将牺牲在敌人的刺刀下。现在Lucky用心灵感应向你发出求助:要保住自己的性命并且歼灭所有敌人,Lucky最多只能用多少时间给枪装上一发子弹?
说明:假设一开始Lucky的枪中就有一发子弹,并且一旦确定一个装弹时间,Lucky始终会用这个时间完成子弹的装卸。希望你能帮助Lucky脱离险境。
输入
每组输入数据,第一行有两个整数n和m,(2≤n≤100,000; 1≤m≤10,000,000)n代表敌人个数,m代表Lucky的射程。
接下来有n行,每行一个整数mi,(1≤mi≤10,000,000),代表每个敌人一开始相对山头的距离(单位为米)。
输出
每组输出数据仅有一个整数,代表Lucky的换弹时间(单位为秒)。
样例输入 Copy
6 100 236 120 120 120 120 120
样例输出 Copy
25
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
const int mod = 10007;
int n,a[N],k;
bool check(int x)
{
int t=0;
for(int i=1; i<=n; i++)
{
if(a[i]<t)
return 0;
if(a[i]<=t+k)
t+=x;
else
{
t+=(a[i]-t-k);
t+=x;
}
}
return 1;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
scanf("%d",a+i);
sort(a+1,a+1+n);
int l=0,r=10000000;
while(l<r)
{
int mid=(l+r+1)>>1;
if(check(mid)) l=mid;
else r=mid-1;
}
printf("%d",l);
return 0;
}
问题 J: Buy an Integer
时间限制: 1 Sec 内存限制: 128 MB
[提交] [状态]题目描述
Takahashi has come to an integer shop to buy an integer.
The shop sells the integers from 1 through 109. The integer N is sold for A×N+B×d(N) yen (the currency of Japan), where d(N) is the number of digits in the decimal notation of N.
Find the largest integer that Takahashi can buy when he has X yen. If no integer can be bought, print 0.
Constraints
·All values in input are integers.
·1≤A≤109
·1≤B≤109
·1≤X≤1018输入
Input is given from Standard Input in the following format:
A B X输出
Print the greatest integer that Takahashi can buy. If no integer can be bought, print 0.
样例输入 Copy
【样例1】 10 7 100 【样例2】 2 1 100000000000 【样例3】 1000000000 1000000000 100 【样例4】 1234 56789 314159265
样例输出 Copy
【样例1】 9 【样例2】 1000000000 【样例3】 0 【样例4】 254309
提示
样例1解释
The integer 9 is sold for 10×9+7×1=97 yen, and this is the greatest integer that can be bought. Some of the other integers are sold for the following prices:
·10:10×10+7×2=114 yen
·100:10×100+7×3=1021 yen
·12345:10×12345+7×5=123485 yen
样例2解释
He can buy the largest integer that is sold. Note that input may not fit into a 32-bit integer type.
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
#define fi first
#define se second
#define pb push_back
const double eps=1e-6;
const double pi=cos(-1);
const int N=1e5+5;
const int mod=1e9+7;
ll a,b,x;
int solve(ll x)
{
if(x==0)return 1;
int cnt=0;
while(x){
x/=10;
cnt++;
}return cnt;
}
int main()
{
cin>>a>>b>>x;
ll l=0,r=1e9;
while(l<r){
ll mid=(l+r+1)>>1;
if(a*mid+b*solve(mid)<=x)l=mid;
else r=mid-1;
}cout<<l<<endl;
return 0;
}
题解
//https://blog.csdn.net/rhkjcbggvsv/article/details/104915495
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 500;
const int mod = 10007;
int a[105],b[105];
int n,maxn=0;
int check(int x)
{
if(x<0) return 0;
b[1]=max(a[1]-x,1);
for(int i=2;i<=n;i++)
{
if(a[i]+x<=b[i-1]) return 0; //此树加了x也不够前一棵高,所以要把x增加
b[i]=max(b[i-1]+1,a[i]-x);//让当前树矮一点
}
return 1;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",a+i);
maxn=max(maxn,a[i]);
}
int l=-1,r=maxn+3,mid;
while(r>l+1)
{
mid=(l+r)/2;
if(check(mid)) r=mid;
else l=mid;
}
printf("%d",r);
return 0;
}