牛客 - 牛牛与牛妹的约会(贪心)
生活随笔
收集整理的這篇文章主要介紹了
牛客 - 牛牛与牛妹的约会(贪心)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:初始時給出 x 和 y 兩個點,都在 x 軸上,現在要讓點 x 盡可能快的去到點 y 的位置,x 可以有兩種操作:
題目分析:直接貪心做就行了,每次計算出閃現后的位置,如果閃現后的位置與點 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; }?
總結
以上是生活随笔為你收集整理的牛客 - 牛牛与牛妹的约会(贪心)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛客 - Hash(思维+进制转换)
- 下一篇: 2019ICPC(徐州) - Loli,