原题单链接:https://www.luogu.com.cn/training/111#problems
AcWing789. 数的范围
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, q, a[N];
int bs1(int x)
{
int l = -1, r = n;
while (l + 1 != r)
{
int mid = l + r >> 1;
if (a[mid] < x)
l = mid;
else
r = mid;
}
if (a[r] != x)
r = -1;
return r;
}
int bs2(int x)
{
int l = -1, r = n;
while (l + 1 != r)
{
int mid = l + r >> 1;
if (a[mid] <= x)
l = mid;
else
r = mid;
}
if (l != -1 && a[l] == x)
return l;
return -1;
}
int main()
{
cin >> n >> q;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
while (q--)
{
int x;
cin >> x;
int res1 = bs1(x);
if (res1 == -1)
{
cout << "-1 -1\n";
continue;
}
int res2 = bs2(x);
if (res2 == -1)
{
cout << "-1 -1\n";
continue;
}
cout << res1 << ' ' << res2 << endl;
}
return 0;
}
AcWing790.数的三次方程
#include<bits/stdc++.h>
using namespace std;
double n;
int main()
{
cin>>n;
double l=-100,r=100;
// while(r-l>1e-8)
for(int i=0;i<100;i++)
{
double mid=(l+r)/2;
if(mid*mid*mid<=n)l=mid;
else r=mid;
}
printf("%.6f\n",l);
printf("%.6f\n",r);
return 0;
}
P2249【深基13.例1】查找
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int n,m;
int a[N];
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
while (m--)
{
int x;
cin>>x;
int l=0,r=n+1;
while (l+1!=r) // l+1<r
{
int mid=l+r>>1;
if(a[mid]<x) l=mid;
else r=mid;
}
if(a[r]==x) cout<<r<<' ';
else cout<<-1<<' ';
}
return 0;
}
P1102A-B 数对
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int n,C;
int q[N];
int bs1(int x)
{
int l=0,r=n+1;
while(l+1<r)
{
int mid=(l+r)/2;
if(q[mid]<x) l=mid;
else r=mid;
}
if(q[r]==x&&r!=n+1)return r;
else return -1;
}
int bs2(int x)
{
int l=0,r=n+1;
while(l+1<r)
{
int mid=(l+r)/2;
if(q[mid]<=x) l=mid;
else r=mid;
}
if(q[l]==x&&l!=0)return l;
else return -1;
}
int main()
{
cin>>n>>C;
for(int i=1;i<=n;i++)
{
cin>>q[i];
}
sort(q+1,q+n+1);
long long cnt=0;
for(int B=1;B<=n;B++)
{
int A=q[B]+C;
int res1=bs1(A);
if(res1==-1)continue;
int res2=bs2(A);
cnt+=res2-res1+1;
}
cout<<cnt<<endl;
return 0;
}
P1873[COCI 2011/2012 #5] EKO / 砍树
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int n,m;
int q[N];
bool check(int x)
{
long long sum=0;
for(int i=1;i<=n;i++)
{
sum+=max(0,q[i]-x);
// if(sum>=m)return true;
}
if(sum>=m)return true;
else return false;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>q[i];
}
int l=0,r=4e5;
while(l+1<r)
{
int mid=(l+r)/2;
if(check(mid))l=mid;
else r=mid;
}
if(check(r))cout<<r<<endl;
else cout<<l<<endl;
return 0;
}
P1024[NOIP 2001 提高组] 一元三次方程求解
P1678烦恼的高考志愿
P2440木材加工
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, k;
int q[N];
bool check(int x)
{
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum += q[i] / x;
}
if (sum >= k)
return true;
else
return false;
}
int main()
{
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
cin >> q[i];
}
int res = 0, sum = 0;
int l = 0, r = 1e8;
while (l + 1 < r)
{
int mid = (l + r) / 2;
if (check(mid))
l = mid;
else
r = mid;
}
if (check(r))cout << r << endl;
else cout << l << endl;
return 0;
}
P2678[NOIP 2015 提高组] 跳石头
#include<bits/stdc++.h>
using namespace std;
const int N=5e4+10;
int L,n,m;
int q[N];
bool check(int x)
{
int cnt=0;
int i=0,now=0;
while(i<n+1)
{
i++;
if(q[i]-q[now]<x) cnt++;
else now=i;
}
if(cnt<=m)return true;
else return false;
}
int main()
{
std::ios::sync_with_stdio(0);
std::cin.tie(0);
cin>>L>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>q[i];
}
q[n+1]=L;
int l=0,r=L+1;
while(l+1<r)
{
int mid=(l+r)/2;
if(check(mid))l=mid;
else r=mid;
}
if(check(r))cout<<r<<'\n';
else cout<<l<<'\n';
return 0;
}
P3853[TJOI2007] 路标设置
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int L,n,k;
int q[N],s[N];
bool check(int x)
{
int cnt=0;
for(int i=1;i<=n+1;i++)
{
if(s[i]>x)
{
cnt++;
int num=s[i]-x;
while (num>x)
{
cnt++;
num-=x;
}
// cnt+=num/x-1;
// if(num%x)cnt++;
}
}
if(cnt<=k)return true;
return false;
}
int main()
{
cin>>L>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>q[i];
s[i]=q[i]-q[i-1];
}
q[n+1]=L;
s[n+1]=L-q[n];
int l=0,r=1e7+1;
while(l+1<r)
{
int mid=(l+r)/2;
if(check(mid))r=mid;
else l=mid;
}
if(check(l))cout<<l<<'\n';
else cout<<r<<'\n';
return 0;
}