牛客 - 牛牛与牛妹的约会(贪心)

题目链接:点击查看

题目大意:初始时给出 x 和 y 两个点,都在 x 轴上,现在要让点 x 尽可能快的去到点 y 的位置,x 可以有两种操作:

  1. 走任意时间的距离,如果花费了 time 的时间,那么可以走 time 的距离
  2. 花费一个单位时间,从点 x 闪现到点 cbrt( x ) ,cbrt是立方根

题目分析:直接贪心做就行了,每次计算出闪现后的位置,如果闪现后的位置与点 y 的距离和闪现前的位置与点 y 的距离相差大于 1,那么显然是选择闪现最优

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<unordered_map>
using namespace std;
    
typedef long long LL;
   
typedef unsigned long long ull;
    
const int inf=0x3f3f3f3f;
    
const int N=1e5+100;

const double eps=1e-10;

int sgn(double x){
	if(fabs(x) < eps)return 0;
	if(x < 0)return -1;
	else return 1;
}
 
int main()
{
//#ifndef ONLINE_JUDGE
//  freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
//#endif
//  ios::sync_with_stdio(false);
    int w;
    cin>>w;
    while(w--)
    {
    	double x,y,ans=0;
    	scanf("%lf%lf",&x,&y);
    	double xx=pow(x,1.0/3);
    	while(1)
    	{
    		double xx=cbrt(x);
    		if(sgn(fabs(xx-y)-fabs(x-y)+1.0)<0)
    		{
    			x=xx;
    			ans+=1.0;
			}
    		else
    		{
    			ans+=fabs(x-y);
    			break;
			}
		}
		printf("%.10f\n",ans);
	}
    
    
    
    
    
    
    
    
    
    
    
    
     
     
       
       
       
       
       
       
       
    return 0;
}
发布了646 篇原创文章 · 获赞 20 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_45458915/article/details/104306122