[贪心]JZOJ 5885 物理实验

Description

 

Input

Output

 

Sample Input

1
5 5
1 2 5 8 9

Sample Output

0.75
 

Data Constraint

分析

贪心,用队列维护可行的z和x即可

 

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=1e5+10;
int a[N];
int t,n,m;
double f;

int main() {
    freopen("atom.in","r",stdin);
    freopen("atom.out","w",stdout);
    scanf("%d",&t);
    while (t--) {
        scanf("%d%d",&n,&m);
        for (int i=1;i<=n;i++) scanf("%d",&a[i]);
        sort(a+1,a+n+1);
        f=0.0;
        int t=n;
        for (int i=n-2;i;i--) {
            while (a[i]+m<a[t]&&t>=i+2) t--;
            if (t==i+1) continue;
            f=max(f,(double)(a[t]-a[i+1])/(double)(a[t]-a[i]));
        }
        printf("%.9lf\n",f==0?-1:f);
    }
    fclose(stdin);fclose(stdout);
}
View Code

 

posted @ 2018-10-14 20:28  Vagari  阅读(115)  评论(0编辑  收藏  举报