Luo Gu P2571 [SCOI2010] belt

Topic links:

kma

Topic analysis:

Bare-third sets of three points ah, inner-thirds Why is unimodal can see proof of this blog, the feeling is the most clearly written an article

→ Team who won FSYolanda beating and hanging

But in fact, the beginning is not quite get to know the most three-point demand unimodal function value is Gesha thing, so here it is recorded
Hand drawing, do not Tucao how ugly
TIM picture 20190808004033.jpg

Code:

#include <bits/stdc++.h>
#define N (2000 + 10)
using namespace std;
inline int read() {
    int cnt = 0, f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == '-') f = -f; c = getchar();}
    while (isdigit(c)) {cnt = (cnt << 3) + (cnt << 1) + c - '0'; c = getchar();}
    return cnt * f;
}
int x, y;
int P, Q, R;
const double eps = 1e-8;
int dcmp(double x) {
    if (fabs(x) < eps) return 0;
    else return x < 0 ? -1 : 1;
}
struct Vector{
    double x; double y;
    Vector (){};
    Vector (double x_, double y_) : x (x_), y (y_) { };
}A, B, C, D;
typedef Vector Point;
struct Line{
    Point u; Point v;
    Line (Point u_, Point v_) : u (u_), v(v_) { };
};
typedef Line Segment;

Vector operator + (Vector A, Vector B) {
    return Vector (A.x + B.x, A.y + B.y);
}

Vector operator - (Vector A, Vector B) { 
    return Vector (A.x - B.x, A.y - B.y);
}

Vector operator / (Vector A, double p) {
    return Vector (A.x / p, A.y / p);
}

double Dot (Vector a, Vector b) {
    return a.x * b.x + a.y * b.y;
}
double Length(Vector A){
    return sqrt(Dot(A, A));
}

double calc(Point X) {
    Point l = C, r = D;
    while (Length(l - r) > eps) {
        Point x = (r - l) / 3;
        Point lmid = l + x, rmid = r - x;
        double ans1 = Length(lmid - D) / Q + Length(X - lmid) / R;
        double ans2 = Length(rmid - D) / Q + Length(X - rmid) / R;
        if (ans2 - ans1 > eps) r = rmid;
        else l = lmid;
    }
    return Length(l - D) / Q + Length(X - l) / R;
}
double solve() {
    Point l = A, r = B;
    while (Length(l - r) > eps) {
        Point x = (r - l) / 3;
        Point lmid = l + x, rmid = r - x;
        double ans1 = calc(lmid) + Length(lmid - A) / P;
        double ans2 = calc(rmid) + Length(rmid - A) / P;
        if (ans2 - ans1 > eps) r = rmid;
        else l = lmid;
    } return calc(l) + Length(l - A) / P;
}
int main() {
    x = read(), y = read();
    A = Point(x, y);
    x = read(), y = read();
    B = Point(x, y);
    x = read(), y = read();
    C = Point(x, y);
    x = read(), y = read();
    D = Point(x, y);
    P = read(), Q = read(), R = read();
    return printf("%0.2lf", solve()), 0;
}

Guess you like

Origin www.cnblogs.com/kma093/p/11318759.html