二分+三分

The Frog‘s Games

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 4218    Accepted Submission(s): 2051

Problem Description

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

Source

The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
int L,n,m,a[500005],b[500005];
int main()
{
	while(scanf("%d%d%d",&L,&n,&m)!=EOF)
	{
		int temp=0,dis=0;
		a[0]=0;
		for(int i=1;i<=n;i++)
			scanf("%d",&a[i]);
		a[n+1]=L;
		sort(a,a+1+n);
		for(int i=1;i<=n+1;i++)
		{
			b[++temp]=a[i]-a[i-1];
			if(b[temp]>dis)
				dis=b[temp];
		}
		int l,r,mid,ans=0;
		l=dis,r=L;
		while(l<=r)
		{
			int tp=0,num=0;
			mid=(l+r)>>1;
			for(int i=1;i<=temp;i++)
			{
				if(tp+b[i]>mid)
				{
					num++;
					tp=b[i];
				}
				else
				{
					tp+=b[i];
				}
			}
			num++;
			if(num<=m)
			{
				ans=mid;
				r=mid-1;
			}
			else
				l=mid+1;
		}
		printf("%d\n",ans);
	}
	return 0;
}

  

时间: 2024-11-05 16:07:16

二分+三分的相关文章

Toxophily-数论以及二分三分

G - Toxophily Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 2298 Description The recreation center of WHU ACM Team has indoor billiards, Ping Pang, chess and bridge, toxophily, deluxe ballroom

uva 1463 - Largest Empty Circle on a Segment(二分+三分+几何)

题目链接:uva 1463 - Largest Empty Circle on a Segment 二分半径,对于每个半径,用三分求出线段到线段的最短距离,根据最短距离可以确定当前R下每条线段在[0,L]上的可行区间,存在一个点被可行区间覆盖n次. #include <cstdio> #include <cstring> #include <cmath> #include <vector> #include <algorithm> using n

Hdu 2899 - Strange fuction 二分/三分求函数极值点

Strange fuction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4527    Accepted Submission(s): 3251 Problem Description Now, here is a fuction: F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=

二分三分总结

一.二分 二分最基本的用途是用来对一个有序数列二分查找元素,由于二分的时间复杂度仅为O(log n),又是还可以用于枚举可能的答案进行判定,即为二分答案.考试时考的最多的也是二分答案(毕竟二分查找都有STL写好的函数了...). 二分答案题型总结: 能用二分答案做的题一般都有单调性,即在某个限度内,劣与这个限度的答案都满足题意但不是最优,而比这个限度最强的答案则不能满足题意.我们就要通过二分的方法把这个限度求出来.例如最大值最小化问题(或最小值最大化问题),和一部分最优化问题. 二分答案的优点在

二分三分

这一部分还是二分好用(因为二分好写) 三分在求非单调函数极值有大用 二分主要思路就是每次将当前区间分为两部分,当前接肯定在中点,左区间与右区间之中,那么把不合理的区间抛弃,就可以极快地求出解 例题: 愤怒的牛(很像跳石头) 农夫 John 建造了一座很长的畜栏,它包括N (2 ≤ N ≤ 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 ≤ xi ≤ 1,000,000,000). 但是,John的C (2 ≤ C ≤ N)头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他

HDU 2141(二分&amp;三分 _B题)解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 ----------------------------------------------------------------------------------- 题意:三个数组,找到每个数组中加和为M的值的组数. 思路:首先将后两个数组加和 O(N^2),排序 O(NlogN),然后利用二分求解. 代码: #include<cstdio> #include<cstring>

暑假集训 || 二分+三分

当发现答案是单调的,从已知条件推答案不太容易,而假设知道了答案,推已知量很容易,这时可以二分 二分答案,根据判断答案不断缩小答案所在区间,最终得到答案 细节很烦.. //lower_bound(arr, arr+n, x) == [l, r)中>=val的第一个元素位置//upper_bound(arr, arr+n, x) == [l, r)中>val的第一个元素位置 POJ 3579 题意:给n个数,这n个数中间一共有C(n,2)个差值,求这些差值的中间值 思路:1e5的数据,因为差值最小

HDU2899Strange fuction(二分/三分)

传送门 题目大意:求 F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100):的最小值 题解:求个导,二分导函数零点,就是原函数最小值所在的x. #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #define LL long long using names

HDU 2199 (二分&amp;三分 _A题)解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2199 ----------------------------------------------------------------------------------- 题意:8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,求解 思路:判断下导数,二分法求解. 代码: #include<cstdio> #include<cstring> #include&