10.10T4 糖水不等式+贪心
明显根据糖水不等式Ay,Ax一定时,因为分母一定大于分子,所以Az越大越好,同理Ax,Ay的差距越小越好
于是我们确定左边 i 为Ax,不超边界的情况下i+1就是Ay,然后upper_bound查Az
注意精度
code:
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 using namespace std; 5 int a[100005]; 6 int read(){ 7 int x=0,f=1; 8 char c=getchar(); 9 while(!isdigit(c)){ 10 if(c=='-')f=-1; 11 c=getchar(); 12 } 13 while(isdigit(c)){ 14 x=(x<<1)+(x<<3)+c-'0'; 15 c=getchar(); 16 } 17 return x*f; 18 } 19 int main(){ 20 int T; 21 cin>>T; 22 while(T--){ 23 double ans=-1; 24 int n,m; 25 n=read(),m=read(); 26 for(int i=1;i<=n;i++)a[i]=read(); 27 sort(a+1,a+n+1); 28 int head=1; 29 for(int i=1;i<=n-2;i++){ 30 if(a[i+2]>a[i]+m)continue; 31 head=upper_bound(a+1,a+n+1,a[i]+m)-a-1; 32 ans=max(ans,double(a[head]-a[i+1])/(a[head]-a[i])*1.0); 33 } 34 if(ans<0){ 35 cout<<-1<<'\n'; 36 continue; 37 } 38 printf("%.9lf\n",ans); 39 } 40 return 0; 41 }
over