Can you find it? (三分搜索

来源:互联网 发布:淘宝申请退货 多长时间 编辑:程序博客网 时间:2024/06/03 18:16


Can you find it?

Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others)Total Submission(s): 1199 Accepted Submission(s): 383
Problem Description
Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X.
 

Input
There are many cases. Every data case is described as followed: In the first line there are three integers L, N, M, in the second line there are L integers represent the sequence A, in the third line there are N integers represent the sequences B, in the forth line there are M integers represent the sequence C. In the fifth line there is an integer S represents there are S integers X to be calculated. 1<=L, N, M<=500, 1<=S<=1000. all the integers are 32-integers.
 

Output
For each case, firstly you have to print the case number as the form "Case d:", then for the S queries, you calculate if the formula can be satisfied or not. If satisfied, you print "YES", otherwise print "NO".
 

Sample Input
3 3 31 2 31 2 31 2 331410
 

Sample Output
Case 1:NOYESNO
 



二分搜索搜索的是线性的,三分搜索搜索的是二次函数性质的(求最大值||最小值。
题意:
给你一个平面,平面上有两条线段AB和CD,某东西在AB上跑的速度是P,在CD上跑的速度是Q,在平面其他地方跑的速度是R,问从A点到D点的最快时间。
思路:
嵌套的三分搜索
1.首先明确需要在AB和CD上分别确定一个点作为转折点。
2.若已知AB,CD上的定点P1,P2,那么Time = AP1/P + P1P2/R + P2D/Q;
3.若我们能够先知道点P1(x1,y1),那么可以根据P2(x2,y2)求出最快的时间Time。
Time = AP1/P + sqrt((x1-x2)^2 + (y1-y2)^2)/R + sqrt((x2-D.x) + (y2-D.y))/Q;
#include <iostream>#include <algorithm>#include <string.h>#include <cmath>#include <stdio.h>#include <vector>#include <map>#include <queue>#include <utility>typedef long long ll;using namespace std;struct Point {    double x, y;    Point(double x = 0.0, double y = 0.0):x(x), y(y){}    Point& operator = (const Point& p) {        this->x = p.x;        this->y = p.y;        return *this;    }};Point A, B, C, D;int P, Q, R;Point Get_Point (const Point& x, const Point& y) {    return Point((x.x + y.x)/2.0, (x.y + y.y)/2.0);}double Get_Len (const Point& x,const Point& y) {    return sqrt((x.x - y.x)*(x.x - y.x) + (x.y - y.y)*(x.y - y.y));}double Get_Time (const Point& p) {    Point left = C, right = D, mid, mmid;    double mid_time = 0, mmid_time = 1;    while (fabs(mmid_time - mid_time) > 1e-6) {        mid = Get_Point(left, right);        mmid = Get_Point(mid, right);                mid_time = Get_Len(p, mid)/R + Get_Len(D, mid)/Q;        mmid_time = Get_Len(p, mmid)/R + Get_Len(D, mmid)/Q;                if (mid_time - mmid_time >= 1e-6)            left = mid;        else            right = mmid;    }    return mid_time + Get_Len(A, p)/P;}int main () {    int T;    cin >> T;    while (T--) {        cin >> A.x >> A.y >> B.x >> B.y >> C.x >> C.y >> D.x >> D.y >> P >> Q >> R;                Point left(A.x, A.y), right(B.x, B.y);        double mid_time = 0, mmid_time = 1;                while (fabs(mid_time - mmid_time) > 1e-6) {            Point mid = Get_Point(left, right);            Point mmid = Get_Point(mid, right);                        mid_time = Get_Time(mid);            mmid_time = Get_Time(mmid);                        if (mid_time - mmid_time >= 1e-6)                left = mid;            else                right = mmid;        }        printf("%.2lf\n", mid_time);    }}


0 0
原创粉丝点击