生活随笔
收集整理的這篇文章主要介紹了
2020ICPC(上海) - Walker(分类讨论+二分)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:在長度為 n 的數軸上給出兩個人的初始位置和速度,問使得每個位置至少被一個人走過的時間是多少
題目分析:分類討論題目,分四種情況討論即可,設 p1 < p2:
p1 走完全程p2 走完全程p1 向右走,p2 向左走p1 向左走,p2 向右走,最后共同走完 p1 ~ p2 這段區間
前三種都可以推出公式快速得出,對于第四種需要二分確定答案,可以二分斷點位置 x,這樣問題就變成了:p1 走完 [ 0 , x ] 這段區間,p2 走完 [ x , n ] 這段區間,類比于前三種情況貪心去討論就好了
為了避免二分被卡精度,可以計算一下時間復雜度將 while 改成 for 即可
代碼:
?
// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#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<cassert>
#include<bitset>
#include<unordered_map>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e6+100;const double eps=1e-10;int sgn(double x)
{if(fabs(x)<=eps)return 0;else if(x<0)return -1;elsereturn 1;
}int main()
{
#ifndef ONLINE_JUDGE
// freopen("data.in.txt","r",stdin);
// freopen("data.out.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);int w;cin>>w;while(w--){double n,p1,p2,v1,v2;scanf("%lf%lf%lf%lf%lf",&n,&p1,&v1,&p2,&v2);if(sgn(p1-p2)>0){swap(p1,p2);swap(v1,v2);}double ans=1e10;ans=min(ans,max((n-p1)/v1,p2/v2));//p1向右,p2向左ans=min(ans,(min(n-p1,p1)+n)/v1);//p1走全程ans=min(ans,(min(n-p2,p2)+n)/v2);//p2走全程double l=p1,r=p2;auto check=[&](double p){double t1=(min(p-p1,p1)+p)/v1;double t2=(min(p2-p,n-p2)+(n-p))/v2;ans=min(ans,max(t1,t2));return sgn(t1-t2)<0;};for(int t=0;t<1000;t++)//二分分界點{double mid=(l+r)/2;if(check(mid))//p1先到分界點l=mid;//分界點右移elser=mid;}printf("%.10f\n",ans);}return 0;
}
?
總結
以上是生活随笔為你收集整理的2020ICPC(上海) - Walker(分类讨论+二分)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。