二分算法

001:Aggressive cows

总时间限制: 1000ms 内存限制: 65536kB
描述
Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,…,xN (0 <= xi <= 1,000,000,000).

His C (2 <= C <= N) cows don’t like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?
输入

  • Line 1: Two space-separated integers: N and C

  • Lines 2…N+1: Line i+1 contains an integer stall location, xi
    输出

  • Line 1: One integer: the largest minimum distance
    样例输入

5 3
1
2
8
4
9

样例输出

3

提示
OUTPUT DETAILS:

FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3.

Huge input data,scanf is recommended.

解题思路
二分查找
找到牛距离的最大值,即总距离除以(牛的数量-1);
最小值0,疯狂二分即可;
记得先排序才能二分;

#include<iostream>
#include<algorithm>
using namespace std;

int Distance[100010];

int main()
{	
	int Snum,Cnum;
	cin>>Snum>>Cnum;
	for (int i =0;i<Snum;i++){
		cin>>Distance[i];
	}
	sort(Distance,Distance+Snum);
	int Max=Distance[Snum-1];
	int Min=Distance[0];
	int R=(Max-Min)/(Cnum-1);
	int L=0;
	int Mid;
	
	while (L<=R){
		Mid = L+(R-L)/2; 
		cout<<"Mid "<<Mid<<endl;
		int count=1;
		int sum=0;

		for(int i=0;i<Snum-1;i++){
			if (sum+Distance[i+1]-Distance[i]>=Mid)
				{
				count++;
				sum=0;}
			else sum+=Distance[i+1]-Distance[i];
		}
	if(count>=Cnum)    L=Mid+1;     //可以放更多的牛
	else      R=Mid-1;            //放不下那么多牛
	}
	cout<<R<<endl;            
		return 0;
	
 } 

出来时若count>=Cnum.则left++>R,输出R即可;
出来时若count<Cnum,R<left,也输出R即可;
因为是求最大值;所以相等时依旧可以尝试更小的距离;
若是查找排序数组位置,即可以添加==的条件,输出mid即可;

002:派

总时间限制: 1000ms 内存限制: 65536kB
描述
我的生日要到了!根据习俗,我需要将一些派分给大家。我有N个不同口味、不同大小的派。有F个朋友会来参加我的派对,每个人会拿到一块派(必须一个派的一块,不能由几个派的小块拼成;可以是一整个派)。

我的朋友们都特别小气,如果有人拿到更大的一块,就会开始抱怨。因此所有人拿到的派是同样大小的(但不需要是同样形状的),虽然这样有些派会被浪费,但总比搞砸整个派对好。当然,我也要给自己留一块,而这一块也要和其他人的同样大小。

请问我们每个人拿到的派最大是多少?每个派都是一个高为1,半径不等的圆柱体。

输入
第一行包含两个正整数N和F,1 ≤ N, F ≤ 10 000,表示派的数量和朋友的数量。
第二行包含N个1到10000之间的整数,表示每个派的半径。
输出
输出每个人能得到的最大的派的体积,精确到小数点后三位。
样例输入

3 3
4 3 3

样例输出

25.133

解答
找到pie的体积的最大值,最小值为0;
最大值:最大的那块蛋糕;
需要EPS=1e-6,来确定精度;

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;

double Pi=acos(-1);
double Radi[10010];
double EPS=1e-6;
//判断一个蛋糕可以切几块所需面积
int fenshu(double Vz,double Vi){
	int count=0;
	while (Vz>=Vi){
		Vz-=Vi;   
		count+=1;
	}
	return count;        //可以切的块数
}
int main(){
	int N;
	int F;
	cin>>N>>F;        //派的数量N,朋友数量F
	for (int i =0;i<N;i++){
		cin>>Radi[i];
		Radi[i]=Radi[i]*Radi[i]*Pi;
	}
	sort(Radi,Radi+N);
	double R=Radi[N-1];
	double L=0;
	double Mid;
	int Sum=0;
	
	while (L<=R){
		Mid=L+(R-L)/2;
		Sum=0;
		for (int i=0;i<N;i++){
			Sum+=fenshu(Radi[i],Mid);         //每一块的块数相加
		}
		if (Sum>=F+1) L=Mid+EPS;        
		else R=Mid-EPS;
	}
	printf("%.3lf",R);
	return 0;
}

003:月度开销

总时间限制: 1000ms 内存限制: 65536kB
描述
农夫约翰是一个精明的会计师。他意识到自己可能没有足够的钱来维持农场的运转了。他计算出并记录下了接下来 N (1 ≤ N ≤ 100,000) 天里每天需要的开销。

约翰打算为连续的M (1 ≤ M ≤ N) 个财政周期创建预算案,他把一个财政周期命名为fajo月。每个fajo月包含一天或连续的多天,每天被恰好包含在一个fajo月里。

约翰的目标是合理安排每个fajo月包含的天数,使得开销最多的fajo月的开销尽可能少。

输入
第一行包含两个整数N,M,用单个空格隔开。
接下来N行,每行包含一个1到10000之间的整数,按顺序给出接下来N天里每天的开销。
输出
一个整数,即最大月度开销的最小值。
样例输入

7 5
100
400
300
100
500
101
400

样例输出

500

提示
若约翰将前两天作为一个月,第三、四两天作为一个月,最后三天每天作为一个月,则最大月度开销为500。其他任何分配方案都会比这个值更大。

解题
找到月度开销的最大值和最小值;
不能给月度开销排序!最大值要遍历后得到;

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
long long N;
long long M;
int Budget[100010];
int main(){
	cin>>N;
	cin>>M;
	long long Sum=0;
	int Max=0;               
	for (long long i=0;i<N;i++){
		cin>>Budget[i];
		Sum+=Budget[i];
		if (Max<Budget[i]) Max=Budget[i];
	}
	int L=Max;         //一个财政周期的预算最小值 
	int R=Sum;             //一个财政周期的预算最大值 
	int Mid;
	int Count;
	int month;
	int result=0;
	while(L<=R){
		Mid = L+(R-L)/2;
		Count=0;
		month=0;
		for (int i=0;i<N;i++){
			if (Count+Budget[i]>Mid){  //前月加当月大于开销
				Count=Budget[i];    //count=当月,继续
				month++;       //月度++
			} 
			else Count+=Budget[i];  //小于则继续加
		}
		if(month>=M){           //月度大于所需
			L=Mid+1;        //开销可以增大
		}
		else R=Mid-1;        //开销减小
	}
	cout<<Mid<<endl;    //求最小值,不满足条件说明小了,输出mid即可
	return 0;			
	
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值