日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

三分查找

發(fā)布時(shí)間:2025/4/16 编程问答 10 豆豆
生活随笔 收集整理的這篇文章主要介紹了 三分查找 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

三分查找是二分查找的一個(gè)擴(kuò)展,是基于分治思想的高效查找方法。

三分查找適用于凸函數(shù)或凹函數(shù),它可以取得當(dāng)前函數(shù)的最大值或最小值。

三分搜索實(shí)現(xiàn)主要是判斷midl 和 midr 值的大小,

模板:

double solve(){} double trisection_search(double left, double right){double midl, midr;while(right - left > 1e-7){midl = (left + right) / 2;midr = (midl + right) / 2;if(solve(midl) >= solve(midr)) right = midr;else left = midl;} }

?

例題:http://acm.hdu.edu.cn/showproblem.php?pid=3400

題解:該題很明顯是一道三分搜索的題目,分別搜索ab直線段和bc直線段,同時(shí)進(jìn)行判斷,從而縮小范圍,最后取得最小值。

#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<string> #include<cmath>using namespace std;const double esp = 1e-7; struct Node{double x, y; }a, b, c, d, t1, t2;double p, q, r; double ab, cd;double len(Node a1, Node a2){double x = (a2.x - a1.x) * (a2.x - a1.x);double y = (a2.y - a1.y) * (a2.y - a1.y);double L = sqrt(x + y + esp);return L; } double solve(double cdx){t2.x = c.x + (d.x - c.x) * (cdx / cd);t2.y = c.y + (d.y - c.y) * (cdx / cd);return len(t1, t2) / r + (cd - cdx) / q; }double trisection_search2(double abx){t1.x = a.x + (b.x - a.x) * (abx / ab);t1.y = a.y + (b.y - a.y) * (abx / ab);double left = 0, right = cd; double midl, midr;double ans, tmp1, tmp2;while(right - left > 1e-7){midl = (left + right) / 2;midr = (midl + right) / 2;if((tmp1 = solve(midl)) <= (tmp2 = solve(midr)))right = midr;else left = midl;ans = min(tmp1, tmp2);}return ans + abx / p; }double trisection_search1(double left, double right){double midl, midr;double ans, tmp1, tmp2;while(right - left > 1e-7){midl = (left + right) / 2;midr = (midl + right) / 2;if((tmp1 = trisection_search2(midl)) <= (tmp2 = trisection_search2(midr)))right = midr;else left = midl;ans = min(tmp1, tmp2);}return ans; }int main() {int T;scanf("%d", &T);while(T--){scanf("%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y);scanf("%lf%lf%lf%lf", &c.x, &c.y, &d.x, &d.y);scanf("%lf%lf%lf", &p, &q, &r);ab = len(a, b);cd = len(c, d);double ans = trisection_search1(0, ab);printf("%.2lf\n", ans);}return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的三分查找的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。