您的当前位置:首页正文

二分试题集

来源:九壹网

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define lowbit(x) ((x) & (-x))
#define ar2 array<ll, 2>
#define ar3 array<ll, 3>
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
const int mod1 = 1331;
const ll llinf = 0x3f3f3f3f3f3f3f3f;
typedef pair<ll, ll> PII;
typedef vector<ll> vi;
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
ll n, m, k, q, a[N];
bool check(ll x)
{
    ll now=0;
    ll id=1;
    ll cnt=0;
    while(id<=n+1)
    {
        if(a[id]-a[now]>=x)
        {
            now=id;
        }
        else
        {
            cnt++;
        }
        id++;
    }
    return cnt<=m;
}
void solve()
{
    cin>>k>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    a[n+1]=k;
    ll l=0,r=1e9,res=-1;
    while(l<=r)
    {
        ll mid=(l+r)/2;
        if(check(mid))
        {
            res=mid;
            l=mid+1;
        }
        else
        {
            r=mid-1;
        }
    }
    cout<<res<<endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int _ = 1;
//    cin >> _;
    while (_--)
        solve();
return 0;
}

#include <iostream>
using namespace std;
#define endl '\n'
#define ll long long
ll maxlen(ll l, ll r) {
	ll max = 0;
	ll left = 1, right = r - l + 1;
	while (left <= right) {
		ll mid = left + (right - left) / 2;
		ll end = l + (mid * (mid - 1)) / 2;
		if (end <= r) {
			max = mid;
			left = mid + 1;
		} else {
			right = mid - 1;
		}
	}
	return max;
}
void solve() {
	int t;
	cin >> t;
	while (t--) {
		ll l, r;
		cin >> l >> r;
		cout << maxlen(l, r) << "\n";
	}
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	return 0;
}

 

#include <iostream>
#include <vector>
using namespace std;
#define endl '\n'

bool is_valid(const vector<int>& sushi, int len) {
	int n = sushi.size();
	vector<int> p1(n + 1, 0), p2(n + 1, 0);
	for (int i = 0; i < n; i++) {
		p1[i + 1] = p1[i] + (sushi[i] == 1);
		p2[i + 1] = p2[i] + (sushi[i] == 2);
	}
	int half = len / 2;
	for (int i = 0; i <= n - len; i++) {
		int left_count1 = p1[i + half] - p1[i];
		int left_count2 = p2[i + half] - p2[i];
		int right_count1 = p1[i + len] - p1[i + half];
		int right_count2 = p2[i + len] - p2[i + half];
		if ((left_count1 == half && right_count2 == half) || (left_count2 == half && right_count1 == half)) {
			return true;
		}
	}
	return false;
}

void solve() {
	int n;
	cin >> n;
	vector<int> sushi(n);
	for (int& s : sushi) cin >> s;
	int left = 1, right = n / 2, ans = 0;
	while (left <= right) {
		int mid = left + (right - left) / 2;
		if (is_valid(sushi, 2 * mid)) {
			ans = mid;
			left = mid + 1;
		} else {
			right = mid - 1;
		}
	}
	cout << ans * 2 << "\n";
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	return 0;
}

 

 

#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 2e5+10;
int t;
int n,x;
int a[N];
bool check(LL mid)
{   
    LL cnt = 0;
    for(int i = 1;i<=n;i++)
    {
        if(a[i]<mid)cnt+=mid-a[i];
        if(cnt>x)return false;
    }
    return true;
}
void solve()
{
    cin>>n>>x;
    for(int i = 1;i<=n;i++)cin>>a[i];
    LL l=1,r=2e9+10,ans=-1;
    while(l<=r)
    {
        LL mid = r+l>>1;
        if(check(mid)){
            ans=mid;
            l=mid+1;
        }
        else r=mid-1;
    }
    cout<<ans<<"\n";
}
int main()
{
    cin>>t;
    while(t--)solve();
    return 0;
}

 

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define inf 0x3f3f3f3f3f3f3f3f
#define PII pair<int, int>
const int N = 2e6 + 10;
int n,k,a[N],b[N],s[N];
void solve() {
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        s[i]=s[i-1]+a[i];
    }
    for(int i=1;i<=n;i++){
        cin>>b[i];
    }
    int l=1,r,ans=0;
    for(int i=1;i<=n;i++){
        r=i;
        if(b[i-1]%b[i]!=0)l=r;
        while(s[r]-s[l-1]>k)l++;
        ans=max(ans,r-l+1);
    }
    cout<<ans<<endl;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int _ = 1;
    cin >> _;
    while (_--){
        solve();
    }
    return 0;
}

 

#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<set>
#define int unsigned long long
#define endl '\n'
using namespace std;
typedef pair<int,int> PII;
struct node{
	int t,l,w;
}a[1100];
int n,m;
bool check(int mid){
	int sum=0;
	for(int i=1;i<=n;i++){
		int x=mid;
		int tt=x/(a[i].t*a[i].l+a[i].w);
		sum+=tt*a[i].l;
		x-=(a[i].t*a[i].l+a[i].w)*tt;
		int op=min(x/a[i].t,a[i].l);
		sum+=op;
		if(sum>=m) return true;
	}
	return false;
}
void solve(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i].t>>a[i].l>>a[i].w;
	}
	int l=0,r=2e18;
	while(l<r){
		int mid=(l+r)/2;
		if(check(mid)){
			r=mid;
		}
		else l=mid+1;
	}
	cout<<l<<endl;
}
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int T=1;
	cin>>T;
	while(T--){
		solve();
	}
	return 0;
}

 

#include <iostream>
#include <cmath>
using namespace std;
#define ll long long
ll check(ll R) {
	ll count = 0;
	for (ll x = 0; x * x <= R * R; ++x) {
		ll ymax = sqrt(R * R - x * x);
		if (x == 0) {
			count += (2 * ymax + 1); 
		} else {
			count += (4 * ymax + 2); 
		}
	}
	return count;
}
int main() {
	ll K;
	cin >> K;
	ll left = 0, right = 150000;
	ll result = 0;
	while (left <= right) {
		ll mid = left + (right - left) / 2;
		if (check(mid) >= K) {
			result = mid;
			right = mid - 1;
		} else {
			left = mid + 1;
		}
	}
	cout << result << endl;
	return 0;
}

 

#include<cstdio>
#include<cmath>
int main()
{
	double s,s1,s2,v1,v2,t1,t2,mid;
	double a,b;
	scanf("%lf%lf%lf",&s,&v1,&v2);
	s1=0;
	s2=s;
	do
	{
		mid=(s1+s2)/2.0;
		a=mid/v2;
		b=(mid-a*v1)/(v1+v2);
		t1=a+(s-mid)/v1;
		t2=a+b+(s-(a+b)*v1)/v2;
		if(t1<t2)
		  s2=mid;
		else
		  s1=mid;
	}
	while(fabs(t1-t2)>1e-7);
	printf("%.6lf",t1);
	return 0;
}

 

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void solve() {
	int t;
	cin >> t;
	while (t--) {
		int n, q;
		cin >> n >> q;
		vector<int> a(n);
		for (int& s : a) cin >> s;
		sort(a.rbegin(), a.rend());
		vector<long long> prefix_sum(n + 1, 0);
		for (int i = 0; i < n; i++) {
			prefix_sum[i + 1] = prefix_sum[i] + a[i];
		}
		while (q--) {
			long long x;
			cin >> x;
			int left = 1, right = n, answer = -1;
			while (left <= right) {
				int mid = left + (right - left) / 2;
				if (prefix_sum[mid] >= x) {
					answer = mid;
					right = mid - 1;
				} else {
					left = mid + 1;
				}
			}
			cout << answer << endl;
		}
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	return 0;
}

 

#include <iostream>
using namespace std;
void solve() {
	int t;
	cin >> t;
	while (t--) {
		long long n, k;
		cin >> n >> k;
		long long left = 1, right = k + k / (n - 1);
		long long result = 0;
		while (left <= right) {
			long long mid = left + (right - left) / 2;
			long long count = mid - mid / n;
			if (count >= k) {
				result = mid;
				right = mid - 1;
			} else {
				left = mid + 1;
			}
		}
		cout << result << endl;
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	return 0;
}

 

 

因篇幅问题不能全部显示,请点此查看更多更全内容

Top