二分法专题练习

A - Pie

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define pi acos(-1.0)
const int M=1e5+5;
double a[M];
int c,N;
int check(double x){
	int num=0;
	for(int i = 1 ; i<=N ; i++){
		num+=int(a[i]/x);
	}
	if(num>=c+1){
		return 1;
	}
	else
		return 0;
}
int main()
{
	int T;
	cin>>T;
	for(int q=0 ; q<T ; q++){
		cin>>N>>c;
	double s=0;
	for(int i=1 ; i<=N ; i++){
		int m;
		cin>>m;
		a[i]=m*m*pi;
		s+=a[i];
	}
	sort(a+1,a+1+N);
	double p=s/(c+1);
	double l=0 , r=p,mid;
	while(r-l>1e-6){
		mid=l+(r-l)/2;
		if(check(mid)){
			l=mid;
		}
		else
			r=mid;
	}
	printf("%.4lf\n",mid);
	}
	
}

 E - 借教室

 

#include<iostream>
#include<cstring>
#include<sstream>
#include<stack>
#include<queue>
#include<cmath>
#include<stdio.h>
#include<assert.h>
#include<algorithm>
using namespace std;
#define int long long
const double PI = acos(-1.0);
const int N = 1e6 + 10;
int L, n, m;
int a[N];
struct nod{
    int d, s, t;
}no[N];
int check(int x){
    vector<int> b(n + 10);
    for(int i = 1; i <= x; i++){
        b[no[i].s] += no[i].d;
        b[no[i].t + 1] -= no[i].d;
    }
    //前缀和求教室每天占用几间
    for(int i = 1; i <= n; i++){
        b[i] += b[i - 1];
        if(b[i] > a[i]){
            //申请的超过有的
            return 0;
        }
    }
    return 1;
}


void solve(){
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    for(int i = 1; i <= m; i++){
        cin >> no[i].d >> no[i].s >> no[i].t;
    }
    int l = 1, r = n + 1;
    while(l <= r){
        int mid = (l + r) / 2;
        if(check(mid)){
            l = mid + 1;
        }else{
            r = mid - 1;
        }
    }
    
    if(r != n + 1){
        cout << "-1\n";
        cout << r + 1 << "\n";
        //第一个不符合的人
    }else{
        cout << 0 << "\n";
    }
    
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;
    while(cin >> n >> m){
        solve();
    }
}

 F - 跳石头

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int M=1e5+5;
long long int a[M]={0};
long long int c,N;
int check(int x){
	int num=0,now=0;
	for(int i = 1 ; i<=N+1 ; i++){
		if(a[i]-a[now]<x){
			num++;
		}
		else
			now=i;
	}
	if(num>c){
		return 0;
	}
	else
		return 1;
}
int main()
{
	long long int L;
	cin>>L>>N>>c;
	int s=0;
	for(int i=1 ; i<=N ; i++){
		cin>>a[i];	
	}
	a[N+1]=L;
	int l=1,r=L,mid;
	while(l<=r){
		mid=(r+l)/2;
		if(check(mid)){
			l=mid+1;
		}
		else
			r=mid-1;
	}
	cout<<r;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值