CodeISM Class 14 (Binary Search Problems)
CodeISM Class 14 (Binary Search Problems)
Q: https://codeforces.com/contest/1486/problem/C1
a= [5,1,4,2,3]
l=1,r=5 -> 3
l=3,r=5 -> 5
In interactive problems:
Judge You
(Knows the array) (can ask queries and get answer)
int x; cout<<”? “<<l<<” “<<r<<endl;
cin>>x;
a= [5,1,4,2,3]
int n;
cin>>n;
int l=1,r=5;
cout<<”? “<<l<<” “<<r<<endl;
int x;
cin>>x; // x=3
cout<<”? “<<4<<” “<<5<<endl;
cin>>x; // x=4
int ans;
cout<<”! “<<ans<<endl;
1……….n
(1,n) -> z
int mid = (n+1)/2;
(1,mid) and (mid+1,n)
a1,a2,a3,a4 -> (a1,a2) (a3,a4) -> =z
(a2,a4) (a1,a3) ->!=z
(a2,a3) (a1,a4) -> !=z
a2 is in first half(1...mid)
(1,mid) -> x
Sol:
map<pair<int,int>,int> m;
int query(int l,int r)
{
if(m.find(mp(l,r))!=m.end()){
return m[mp(l,r)];
}
cout<<"? "<<l<<" "<<r<<endl;
int z;
cin>>z;
m[mp(l,r)]=z;
return z;
}
void solve()
{
int n;
cin>>n; //5,1,4,2,3
int l=1,r=n; //l=1,r=3
while(r-l>1){
int mid = (l+r)/2;
int z = query(l,r);
if(z<=mid){ //a2 is present in the first half
int x = query(l,mid);
if(x==z){ //a1 and a2 present in l...mid -> go
left
r=mid;
}else{ // a1 and a2 present in different
halfs....go right
l=mid;
}
}else{ //a2 is present in 2nd half(mid+1...r)
int x = query(mid,r);
if(x==z){ // a1 and a2 present in same
half...mid to r
l=mid;
}else{ // a1 and a2 present in diff
half...go l...mid
r=mid;
}
}
}
int z = query(l,r);
if(z==l){
cout<<"! "<<r<<endl;
}else{
cout<<"! "<<l<<endl;
}
}
Q: https://atcoder.jp/contests/abc174/tasks/abc174_e
#include<bits/stdc++.h>
using namespace std;
int32_t main(){
int n,k;
scanf("%d%d",&n,&k);
vector<int> a(n);
for(int i=0;i<n;i++) cin>>a[i];
int low=1,high=1e9;
// search space-> length of max stick;
int ans=-1;
while(low<=high){
int mid=low+(high-low)/2;
int req_number_of_cuts=0;
for(int i=0;i<n;i++){
if(a[i]%mid!=0)
req_number_of_cuts+=(a[i]/mid);
else
req_number_of_cuts+=((a[i]/mid)-1);
}
if(req_number_of_cuts<=k){
ans=mid;
high=mid-1;
}
else low=mid+1;
}
cout<<ans<<endl;
}
1. If you are given a sorted vector (a[i] <= a[i+1]) and a number x,
and you need to find the index of the first element >=x, this is
called lower_bound of x.
In C++, there is a builtin function to find this in O(log N) using
binary search.
Eg:
int ind = lower_bound(a.begin(), a.end(), x) - a.begin();
2. If you are given a sorted vector (a[i] <= a[i+1]) and a number x,
and you need to find the index of the first element >x, this is
called upper_bound of x.
In C++, there is a builtin function to find this in O(log N) using
binary search.
int ind = upper_bound(a.begin(), a.end(), x) - a.begin();
Suppose, there is no such element, then ind = size of the array.
Q.
https://codeforces.com/edu/course/2/lesson/6/1/practice/contest/2
83911/problem/D
#include <bits/stdc++.h>
int32_t main() {
int n;
cin >> n;
vector<int> vec(n);
sort(vec.begin(), vec.end());
int k;
cin >> k;
int l, r;
while (k--) {
cin >> l >> r;
int y = upper_bound(vec.begin(), vec.end(), r) -
vec.begin();
int x = lower_bound(vec.begin(), vec.end(), l) -
vec.begin();
return 0;
}
Q.
https://codeforces.com/edu/course/2/lesson/6/2/practice/contest/2
83932/problem/E
#include <bits/stdc++.h>
int32_t main() {
// fastio;
ld c;
cin >> c;
if (val <= c) {
lo = mid;
} else {
hi = mid;
}
}
return 0;
}
Q.
https://codeforces.com/edu/course/2/lesson/6/2/practice/contest/2
83932/problem/D
Let f(x) = No. of balloons that can be inflated in x minutes
#include <bits/stdc++.h>
#define int long long
int lo = 0;
int hi = 1e8;
int mid;
int ans = 0;
while (lo <= hi) {
mid = lo + (hi - lo) / 2;
int gx = mid * ti + ((mid - 1) / zi) * yi;
// time taken to fill x=mid balloons
if (gx <= p) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
int32_t main() {
// fastio;
int m, n;
cin >> m >> n;
int lo = 0;
int hi = 1e8;
int mid;
int ans;
vector<int> balloons;
if (num >= m) {
balloons = temp;
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}