二分笔记
猜数字游戏(引入)
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
// 初始化随机数生成器
srand(time(0));
int lower = 1, upper = 100; // 设置猜数字的范围
int number = rand() % (upper - lower + 1) + lower; // 生成一个随机数
int guess, attempts = 0; // 初始化猜测次数
cout << "欢迎来到猜数字游戏!" << endl;
cout << "我想了一个数字,范围是 " << lower << " 到 " << upper << " 之间。" << endl;
// 游戏开始,直到猜对为止
do {
cout << "请输入你的猜测:";
cin >> guess; // 获取用户输入
attempts++; // 增加猜测次数
if (guess > number) {
cout << "你猜的数字太大了!" << endl;
} else if (guess < number) {
cout << "你猜的数字太小了!" << endl;
} else {
cout << "恭喜你,猜对了!" << endl;
cout << "你用了 " << attempts << " 次尝试!" << endl;
}
} while (guess != number); // 直到猜对为止
return 0;
}
整数二分(模板题)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int n, q;
int main() {
scanf("%d %d", &n, &q); // 输入n和q
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
while (q--) {
int op, l, r, x;
scanf("%d %d %d %d", &op, &l, &r, &x);
if (op == 1) {
// 查找等于x最左边的位置
while (l < r) {
int mid = l + r >> 1;
if (x <= a[mid]) {
r = mid;
} else {
l = mid + 1;
}
}
if (a[l] == x) {
printf("%d\n", l);
} else {
printf("%d\n", -1);
}
} else if (op == 2) {// 查找等于x最右边的位置
while (l < r) {
int mid = (l + r + 1) >> 1;
if (x < a[mid]) {
r = mid - 1;
} else {
l = mid;
}
}
if (a[l] == x) {
printf("%d\n", l);
} else {
printf("%d\n", -1);
}
} else if (op == 3) {
// 查找第一个不小于x的元素
if (a[r] < x) {
printf("-1\n");
continue;
}
int idx = lower_bound(a + l, a + r + 1, x) - a;
printf("%d\n", idx);
} else {
// 查找第一个大于x的元素
if (a[r] <= x) {
printf("-1\n");
continue;
}
int idx = upper_bound(a + l, a + r + 1, x) - a;
printf("%d\n", idx);
}
}
return 0;
}
浮点数二分(模板题)
如果题目要求保留
N 位小数,eps一般设置成10^{-(N+2)} 。
#include <iostream>
using namespace std;
double check(double x,double y){
double sum=1;
for(int i=0;i<y;i++)
sum*=x;
return sum;
}
int main()
{
double x,y;
cin >> x >> y;
double l=0,r=10000;
while(r-l>1e-8)
{
double mid=(l+r)/2;
if(check(mid,y)>=x) r=mid;
else l=mid;
}
printf("%.6lf",l);
return 0;
}
二分答案(模板题)
考虑利用小根堆,每次取出序列中最小元素再
+1 ,时间复杂度为O(k\log n) 。一般一秒钟约在10^8 左右,本题k 最大为10^{13} ,会超时
考虑依次枚举最小值,一一验证当前值是否可行,第一个不可行的数
-1 即是最大值。时间复杂度为O(nk) 。
注意到,随着最小值变大,需要消耗的
k 值越多,因此具有单调性关系,可以二分最小值,时间复杂度为O(n\log k) 。需要注意,最坏情况下所有a_i 均是最大值,k 也是最大值,因此二分上界至少为k+\max(a_i) 。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
ll k;
const int N=1e5+10;
ll a[N];
bool check(ll x,ll k){
for(int i=1;i<=n;i++){
if(a[i]-x<0){
k=k+(a[i]-x);
}
if(k<0) return false;
}
return true;
}
int main(){
scanf("%d %lld",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
ll l=1,r=(ll)(1e14);
while(l<r){
ll mid=l+r+1>>1;
if(check(mid,k)){
l=mid;
}else{
r=mid-1;
}
}
printf("%lld",l);
}
分巧克力
考虑从小向大依次枚举巧克力的高度,一一验证,时间复杂度为
O(nk) 。
注意到随着巧克力高度变高,分出来的块数变少,具有单调性,因此可以二分高度。时间复杂度为
O(n\log k)
#include<bits/stdc++.h>
using namespace std;
#define int long long //数据范围要求很大,将int改为long long,可以有效避免超出数据范围
const int N = 1e5+11;
int n,k;
int a[N];
int check(int hight)
{
int nume = 0;
for(int i =0;i <= n;i++)
{
nume += a[i] / hight;
}
return nume>=k;
}
signed main()
{
scanf("%lld %lld",&n,&k);
for(int i = 0;i <= n;i++)
{
scanf("%d",&a[i]);
}
int l = 0; //l代表左范围
int r = 1e9; //r代表右范围
while(l < r)
{
int mid = (l + r + 1)/2; //向下取整
if(check(mid)) //更改区间范围 进一步进行判断
{
l = mid;
}
else
{
r = mid - 1;
}
}
if(l == 0)
{
printf("-1");
}
else
{
printf("%lld",l);
}
return 0;
}
尾0的数量
阶乘尾
0 ,其只有2 和5 可以贡献,而2 的数量一定大于5 。因此,只需要算1\sim N 中有多少个数因子含有5 即可。
很明显,随着
x 变大,其x! 的尾0 数量是会变多的,因此可以直接二分答案。
注意边界,可以通过打表测出
10^{18} 个尾0 的数约为4\times 10^{18} 。
时间复杂度为
#include<bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const ll inf = 6e18;
ll cal(ll mid){
ll ans = 0;
while(mid){
ans += mid / 5;
mid /= 5;
}
return ans;
}
void solve()
{
ll x;
cin >> x;
ll l = 1, r = inf;
while(l < r){
ll mid = l + r >> 1;
if(cal(mid) >= x) r = mid;
else l = mid + 1;
}
if(cal(l) == x) cout << l << endl;
else cout << -1 << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
while(t --) solve();
return 0;
}
端木将军的追击
二分 + 前缀和,枚举第一个山洞,二分第二个山洞即可,时间复杂度为
O(n\log n) 。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> PII;
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const int N = 200010;
int n, m, k;
LL a[N], b[N];
void solve(){
cin >> n >> m >> k;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
a[i] += a[i - 1];
}
for (int i = 1; i <= m; ++i) {
cin >> b[i];
b[i] += b[i - 1];
}
int ans = 0;
for (int i = 0; i <= n; ++i) {
if (a[i] > k) break;
LL res = k - a[i];
int x = upper_bound(b, b + m + 1, res) - b - 1;
ans = max(ans, i + x );
}
cout << ans << '\n';
}
int main(){
ios_base :: sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
while (t--)
{
solve();
}
return 0;
}
数列分段
二分+前缀和
题目要求将长度为
首先观察美味值的公式
值可以用前缀和来求,例如
有这个性质的存在,我们可以在
同时,如果一个序列能够分成
对于 check 函数的编写,我们需要限定每个区间在小于等于
bool check(LL mid) {
int cnt = 1, last = 0;
LL range_sum = 0;
for (int i = 1; i <= n; i ++) {
if (range_sum + (LL)a[i] * (sum[i - 1] - sum[last]) > mid) {
if ( ++ cnt > m) return false;
range_sum = 0;
last = i - 1;
} else {
range_sum += (LL)a[i] * (sum[i - 1] - sum[last]);
}
}
if(cnt!=m) return false;
return true;
}
最终做法为直接二分法答案,也就是二分找到最小的
注意数据范围 1,999,990,000,000,000,000 ,因此二分上界为
#include<cstdio>
typedef long long LL;
const int N = 200010;
int n, m;
int a[N];
LL sum[N];
bool check(LL mid) {
int cnt = 1, last = 0;
LL range_sum = 0;
for (int i = 1; i <= n; i ++) {
if (range_sum + (LL)a[i] * (sum[i - 1] - sum[last]) > mid) {
if ( ++ cnt > m) return false;
range_sum = 0;
last = i - 1;
} else {
range_sum += (LL)a[i] * (sum[i - 1] - sum[last]);
}
}
return true;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++) {
scanf("%d", &a[i]);
sum[i] = sum[i - 1] + a[i];
}
LL l = 0, r = 4e18;
while (l < r) {
LL mid = l + r >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
printf("%lld\n", r);
return 0;
}
第K个二元组
问题分析
直接暴力枚举去求解第
二元组性质分析
假设
(1,1)
(1,2),(2,1)
(1,3),(2,2),(3,1)
(2,3),(3,2)
(3,3)
假设
(1,1)
(1,2),(2,1)
(1,3),(2,2),(3,1)
(1,4),(2,3),(3,2)(4,1)
(2,4),(3,3),(4,2)
(3,4),(4,3)
(4,4)
我们可以发现,以
求解第
这里
- 如果
n\le N ,则公式为n\cdot{1}+\dfrac{n\times(n-1)\cdot 1}{2} 。 - 如果
n> N ,则公式为N\cdot{1}+\dfrac{N\times(N-1)\times 1}{2}+((N-1)\times(n-N)+\dfrac{(n-N)\times(n-N-1)\times -1}{2}) 。
求解第
这样我们可以用二分求解
- 用
K 减去前面的所有元素,得出列号,我们设为C 。
求解二元组
得出列号后,二元组的二个元素情况如下:
- 如果
n\le N ,则i=C,j=n+1-i 。 - 如果
n>N ,则i=n-N+C,j=n+1-i 。
注意,在实际求解中,我们可以求解前
#include <bits/stdc++.h>
using namespace std;
long long n, k, N;
long long calc(long long mid) {
mid = mid - 1;
if (mid <= N) {
return mid + (mid) * (mid - 1) / 2 + 1;
}
return N + N * (N - 1) / 2 + ((N - 1) * (mid - N) - (mid - N) * (mid - N - 1) / 2) + 1;
}
void solve() {
cin >> N >> k;
long long l = 1, r = 2 * N - 1;
while (l < r) {
long long mid = (l + r + 1) / 2;
if (calc(mid) <= k) {
l = mid;
} else {
r = mid - 1;
}
}
n = l;
long long C = k - (calc(l) - 1);
if (n <= N) {
cout << C << " " << (n + 1 - C) << "\n";
} else {
cout << (n - N + C) << " " << (n + 1 - (n - N + C)) <<"\n";
}
}
int main() {
int t;
ios::sync_with_stdio(false);cin.tie(0);
cin >> t;
for (int zu = 1; zu <= t; zu++) {
solve();
}
return 0;
}
参加比赛
贪心+二分+前缀和
需要注意,最后一次比赛是不需要休息的,直接排序可能会出现问题。因此需要先排序,然后枚举最后一场比赛,然后再进行二分
首先对
a_i+b_i 进行排序
预处理出前缀和
s[i]=s[i-1]+a[i]+b[i]
对于当前枚举的最后一场项目,处理剩余时间
k=k-a_i ,然后开始二分,如果二分的位置小于当前项目,则总时间为s_{mid} ,将其与k 做比较,如果位置大于当前项目,需要减去b_i ,即将多计算的比赛时间与休息时间减去,再与k 作比较。
最终,要是二分出来的边界
l ,其值比i 小,需要+1 ,也就是包含本身。
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5;
int n,k,ans;
long long s[N];
struct node {
int et,br;
}item[N];
bool cmp(const node &a, const node &b) {
return a.et + a.br <= b.et + b.br;
}
bool check(int x, int y,int time) {
if(x < y) {
if(s[x] <= time) return true;
else return false;
}
else {
if(x + 1 > n) return false;
if(s[x + 1] - item[y].et - item[y].br <= time) return true;
else return false;
}
}
int main() {
ios::sync_with_stdio(false),cin.tie(0);
cin>>n>>k;
for(int i = 1; i <= n; i ++) cin>>item[i].et;
for(int i = 1; i <= n; i ++) cin>>item[i].br;
sort(item+1,item+1+n,cmp);
for(int i = 1; i <= n; i ++) s[i] = s[i-1] + item[i].et + item[i].br;
for(int i = 1; i <= n; i ++) {
int temp = k;
if(temp < item[i].et) continue;
temp -= item[i].et;
int l = 0, r = n;
while(l + 1 != r) {
int mid = (l + r) >> 1;
if(check(mid,i,temp)) l = mid;
else r = mid;
}
ans = max(l+1,ans);
}
cout<<ans;
return 0;
}
COPY