三分
来源:互联网 发布:mac cad 编辑:程序博客网 时间:2024/04/28 23:31
当需要求凹形或凸行函数的极值,通过函数本身表达式不容易求解时,就可以用三分法不断逼近求解。
简单概念:
在二分查找的基础上,在右区间(或左区间)再进行一次二分,这样的查找算法称为三分查找,也就是三分法。
三分查找通常用来迅速确定最值。
要求::序列为一个凸性或凹形函数。通俗来讲,就是该序列必须有一个最大值(或最小值),在最大值(最小值)的左侧序列,必须满足不严格单调递增(递减),右侧序列必须满足不严格单调递减(递增)。
思路:通过不断缩小 [L,R] 的范围,无限逼近白点。
做法:先取 [L,R] 的中点 mid,再取 [mid,R] 的中点 mmid,通过比较 f(mid) 与 f(mmid) 的大小来缩小范围。
当最后 L=R-1 时,再比较下这两个点的值,我们就找到了答案。
参考程序
int SanFen(int l,int r) //找凸点
{
while(l < r-1)
{
int mid = (l+r)/2;
int mmid = (mid+r)/2;
if( f(mid) > f(mmid) )
r = mmid;
else
l = mid;
}
return f(l) > f(r) ? l : r;
}
例题:
Description
Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di <L).
To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.
Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 ≤ M ≤ N).
FJ wants to know exactly how much he can increase the shortest distance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.
Input
Lines 2..N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.
Output
Sample Input
25 5 2214112117
Sample Output
4
Hint
代码:
#include <iostream>
#include<algorithm>
using namespace std;
int N[50005];
int l,n,m;
int a(int b){
int k=0;
int f;
for(int i=0;i<=n-m;i++){
f=k+1;
while(N[f]-N[k]<b&&f<=(n+1)){
f++;
}
if(f>n+1) return 0;
k=f;
}
return 1;
}
int main()
{
cin>>l>>n>>m;
for(int i=1;i<=n;i++){
cin>>N[i];
}
N[0]=0;
N[n+1]=l;
sort(N,N+n+2);
int z,y;
z=0;
y=l;
if(n==m) cout<<l<<endl;
else {while ((y-z)>1){
int mid=(z+y)/2;
if(a(mid)) z=mid;
else y=mid;
cout<<mid<<" ";
}
cout<<endl;
cout<<z<<endl;
}
return 0;
}
- 三分
- 三分
- 三分
- 三分
- 三分
- 三分
- 三分
- 三分
- 三分
- 三分
- 三分
- 三分
- 三分
- 三分
- 三分
- bzoj1857 三分套三分
- 【BZOJ1857】三分套三分
- 【三分套三分】传送带
- Fox And Two Dots
- 素数求和问题
- APP逆向分析之钉钉抢红包插件的实现-iOS篇
- iOS 内存管理篇(三)---__strong/__weak/__unsafe_unretain/__autoreleasing 认识和使用
- libsvm类型介绍
- 三分
- 常用正则表达式
- golang 模板(template)的常用基本语法
- php中curl的helloworld
- 大话设计模式笔记(六)——原型模式
- java判断字符串是否为数字或中文或字母
- ajax获取动态列表数据后的分页问题
- Nginx--官网中文翻译(中英文对比)--8-命令行参数Command-line parameters
- 美颜效果加后期处理