Can you find it? (三分搜索

本文介绍了一个关于三数之和的问题及其算法实现,并探讨了一种涉及三分搜索的应用场景——寻找两点以达到最短路径的问题。文章通过具体实例展示了如何使用三分搜索来解决这类问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



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 3
1 2 3
1 2 3
1 2 3
3
1
4
10
 

Sample Output
Case 1:
NO
YES
NO
 



二分搜索搜索的是线性的,三分搜索搜索的是二次函数性质的(求最大值||最小值。
题意:
给你一个平面,平面上有两条线段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);
    }
}


统计下文中单词出现的个数存入字典,统计出现字数最多的三个单词和最少三个单词。删除原文中出现次数第二多的单词,其他单词保持顺序不变,先试试删除单词后的文章。 Sports help everyone to keep healthy, happy, and efficient. So 1 pay special attention to games,especially table-tennis. Table tennis is my favorite game. I play it almost every day Table-tennis is an ideal game for us because it brings the whole body into action. It strengthens our muscles, expands our lungs, promotes the circulation of the blood, and causes a healthy action of the skin. Besides, it is very amusing and does not cost us much money. Table-tennis is very moderate; it is not so rough as football. It is an indoor game and can be played even on rainy days. Thus, it is my favorite kind of exercise. One morning a fox sees a cock.He think,"This is my breakfast. He comes up to the cock and says,"I know you can sing very well.Can you sing for me? The cock is glad.He closes his eyes and begins to sing.The fox sees that and catches him in his mouth and carries him away. The people in the field see the fox.They cry,"Look, look!The fox is carrying the cock away. The cock says to the fox,"Mr Fox,do you understand?The people say you are carrying their cock away.Tell them it is yours.Not theirs. The fox opens his mouth and says,"The cock is mine, not yours.Just then the cock runs away from the fox and flies into the tree.多加注释,要流程图
最新发布
06-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值