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

链接:牛牛与牛妹的约会

牛牛去找牛妹,两人都在x轴上,牛牛除了可以以1单位距离/单位时间的速度移动任意时间以外,还可以花费1单位时间进行闪现。每次闪现时,如果当前他的坐标是x=k,他将闪现到 x = k 3 x=\sqrt[3]{k} 的位置。请帮他算算,最短需要多少时间,他可以找到牛妹~

输入描述:

输入数据包括多组用例,输入第一行包含一个数字T ( 1 T 5 × 1 0 5 ) (1 \leq T \leq 5 \times 10^5) ,T表示数据组数。
接下来T行,每行包括两个整数 a , b ( a , b 1 0 6 ) a,b(|a|,|b|\leq 10^6) 表示牛牛所在的位置和牛妹所在的位置。

输出描述:

输出共T行,每行包括一个实数,表示牛牛所花费的最短时间。
如果你的答案是a,标准答案是b,当 a b 1 0 6 |a-b|\leq 10^{-6} 时,你的答案将被判定为正确。

要考虑到负数的情况,这里是开3次方,使用贪心的思想,如果使用闪现前进的距离比直接走的距离小的话,则使用直接前进,否则使用闪现。
pow()函数要注意负数。


#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<list>
#include<set>
#include<cmath>
using namespace std;
typedef long long ll;
#define N 100005
//#define _USE_MATH_DEFINES

int main()
{
	int t;
	cin >> t;
	double a,b;
	double e = 1.0/3.0;
	while(t--){
		scanf("%lf %lf",&a,&b);
		double res = 0,ta=a;
		while(true){
			double na;
			if(a<0) na = -1*pow(-ta,e);
			else na = pow(ta,e);
			if(abs(na-b)<abs(ta-b)-1.0) {
				res += 1.0,ta = na;
				cout << "ta:" << ta << endl;
			}
			else  {
				res += abs(ta-b);
				break;
			}
		}
		printf("%lf\n",res);
	}

    return 0;
}

发布了79 篇原创文章 · 获赞 37 · 访问量 8875

猜你喜欢

转载自blog.csdn.net/SinclairWang/article/details/104303555