2020 cattle off winter algorithm base Camp 5 D. beef and cattle sister appointments

https://ac.nowcoder.com/acm/contest/3006/D

Title Description

Beef after a hard day at the races, cattle sister to go to play, in fact, is also the day of the race cattle sister. In order to find the girl of his cattle to his game from the ground to her game as soon as possible.

Remember, the game is only in the x-axis, so two people meet coordinates y = 0. In addition beef can move time per unit distance rate 1 / unit of time, it also can take a flash unit time. Each time flash, if his current coordinates x=k, he will be flashed to x=\sqrt[3]{k}the position.

Please help him calculate how much time the minimum required, he can find cow girl ~

 Thinking

 Simple greedy, and flash contrast to normal moving speed. There is a card a bit uncomfortable, pow function calculates the cube root if so, can not be negative cube root (?????), otherwise nan, I wa several times because of this, too difficult. Direct calls to library functions cbrt (find the cube root of library functions)

Good for speed calculation, the calculated position of the first arrival of the next flash nexpostion, a distance is calculated using the coordinates of the current position and sister cattle, and then flash the coordinates calculated from several, if the former is subtracted if the latter is greater than 1.0, flash description fast, continues to flash, flash slow or explanation. Do not consider the positive and negative two people are the same, because then the flash will lengthen the distance, the calculation is a negative number, so it satisfies the above requirements.

/*************************************************************************
    > File Name: D.cpp
    > Author: amoscykl
    > Mail: [email protected]
    > Created Time: 2020年02月14日 星期五 14时19分56秒
 ************************************************************************/
 
#include<bits/stdc++.h>
using namespace std;
double sqrt3(double x){
    return cbrt(x);
}
int main(){
    int T;
    scanf("%d", &T);
    while (T--){
        int a, b;
        scanf("%d %d", &a, &b);
        double x = a;
        double y = b;
        double res = 0.0;
        while (1){
            double nex = sqrt3(x);
            double lastdist = abs(x - y);
            double nowdist = abs(nex - y);
            if (lastdist - nowdist > 1.0){
                x = nex;
                res += 1.0;
            }
            else{
                res += lastdist;
                break;
            }
        }
        printf("%.8lf\n", res);
    }
    return 0;
}

 

Published 204 original articles · won praise 13 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43701790/article/details/104313383