二分贪心专题D

The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The width of the river is L (1<= L <= 1000000000). There are n (0<= n <= 500000) stones lined up in a straight line from one side to the other side of the river. The frogs can only jump through the river, but they can land on the stones. If they fall into the river, they 
are out. The frogs was asked to jump at most m (1<= m <= n+1) times. Now the frogs want to know if they want to jump across the river, at least what ability should they have. (That is the frog's longest jump distance).
Input
The input contains several cases. The first line of each case contains three positive integer L, n, and m. 
Then n lines follow. Each stands for the distance from the starting banks to the nth stone, two stone appear in one place is impossible.
Output
For each case, output a integer standing for the frog's ability at least they should have.
Sample Input
6 1 2
2
25 3 3
11 
2
18

Sample Output

4

11

青蛙跳过河,河上有若干个石头,青蛙共跳m次,问青蛙过河一次跳最远的最小值是多少。

方法是二分答案。

首先二分上界是河的宽度 下界是相邻两块石头的最大间距(二分范围尽量缩小,但注意保证正确性)

bool Check(int a[],int n,int m,int x)
{
	int i=0,step=0,j=1;
	bool flag;
	while (j<n)
	{
		flag=true; 
		while (j<n&&a[j]-a[i]<=x)
		{
			j++; 
			flag=false;
		}
		i=j-1;
		if (flag) return false;
		step++; 
		if (step>m) return false;
	}
	return true;
}

对于二分出的答案x,当两块石头的间距小于x时就不断扩大,当最小间距石块不满足时和走过的步数大于m时此方案不可行。

源代码:

#include<iostream>
#include<algorithm>
using namespace std;
int a[500002];

bool Check(int a[],int n,int m,int x)
{
	int i=0,step=0,j=1;
	bool flag;
	while (j<n)
	{
		flag=true; 
		while (j<n&&a[j]-a[i]<=x)
		{
			j++; 
			flag=false;
		}
		i=j-1;
		if (flag) return false;
		step++; 
		if (step>m) return false;
	}
	return true;
}


int main()
{
	a[0]=0; 
	int l,n,m,left,right,i,mid;
	while (cin>>l>>n>>m)
	{
		for (i=1;i<n+1;i++) scanf("%d",&a[i]);
		sort(a,a+n+1); 
		a[n+1]=l, 
		left=a[1];
		right=l;
		n=n+2; 
		while (left<right)
		{
			mid=(left+right)/2;
			if (!Check(a,n,m,mid)) left=mid+1;
			else right=mid; 
		}
		cout<<left<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
信息数据从传统到当代,是一直在变革当中,突如其来的互联网让传统的信息管理看到了革命性的曙光,因为传统信息管理从时效性,还是安全性,还是可操作性等各个方面来讲,遇到了互联网时代才发现能补上自古以来的短板,有效的提升管理的效率和业务水平。传统的管理模式,时间越久管理的内容越多,也需要更多的人来对数据进行整理,并且数据的汇总查询方面效率也是极其的低下,并且数据安全方面永远不会保证安全性能。结合数据内容管理的种种缺点,在互联网时代都可以得到有效的补充。结合先进的互联网技术,开发符合需求的软件,让数据内容管理不管是从录入的及时性,查看的及时性还是汇总分析的及时性,都能让正确率达到最高,管理更加的科学和便捷。本次开发的医院后台管理系统实现了病房管理、病例管理、处方管理、字典管理、公告信息管理、患者管理、药品管理、医生管理、预约医生管理、住院管理、管理员管理等功能。系统用到了关系型数据库中王者MySql作为系统的数据库,有效的对数据进行安全的存储,有效的备份,对数据可靠性方面得到了保证。并且程序也具备程序需求的所有功能,使得操作性还是安全性都大大提高,让医院后台管理系统更能从理念走到现实,确确实实的让人们提升信息处理效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值