牛客 - 牛牛与牛妹的约会(贪心)
生活随笔
收集整理的這篇文章主要介紹了
牛客 - 牛牛与牛妹的约会(贪心)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目鏈接:點(diǎn)擊查看
題目大意:初始時(shí)給出 x 和 y 兩個(gè)點(diǎn),都在 x 軸上,現(xiàn)在要讓點(diǎn) x 盡可能快的去到點(diǎn) y 的位置,x 可以有兩種操作:
題目分析:直接貪心做就行了,每次計(jì)算出閃現(xiàn)后的位置,如果閃現(xiàn)后的位置與點(diǎn) y 的距離和閃現(xiàn)前的位置與點(diǎn) y 的距離相差大于 1,那么顯然是選擇閃現(xiàn)最優(yōu)
代碼:
#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; }?
總結(jié)
以上是生活随笔為你收集整理的牛客 - 牛牛与牛妹的约会(贪心)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 牛客 - Hash(思维+进制转换)
- 下一篇: 2019ICPC(徐州) - Loli,