Codeforces 957C

题目链接  

题意

给出n个数字,和u(之后会用到),然后给出n个数字,为E1,E2,...,En。求(Ei-Ej)/(Ei-Ek)的最大值。其中,Ei-Ej和Ei-Ek都不得大于u且i>j>k

题解

二分搜索的裸题。稍作分析便可知道,要求这个值的最大值,必须要k=j+1,Ei应为小于等于Ej的数的最大值。先枚举j,然后二分搜索出i即可。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<cstdlib>
#include<string>
#include<cstring>
#include<bitset>
#define LL long long
#define mod 1e9+7
#define INF 0x3f3f3f3f
#define van 300001
using namespace std;

namespace FastIO {
    template<typename tp> inline void read(tp &x) {
        x=0; register char c=getchar(); register bool f=0;
        for(;c<'0'||c>'9';f|=(c=='-'),c = getchar());
        for(;c>='0'&&c<='9';x=(x<<3)+(x<<1)+c-'0',c = getchar());
        if(f) x=-x;
    }
    template<typename tp> inline void write(tp x) {
        if (x==0) return (void) (putchar('0'));
        if (x<0) putchar('-'),x=-x;
        int pr[20]; register int cnt=0;
        for (;x;x/=10) pr[++cnt]=x%10;
        while (cnt) putchar(pr[cnt--]+'0');
    }
    template<typename tp> inline void writeln(tp x) {
        write(x);
        putchar('\n');
    }
}
using namespace FastIO;
int i,n,u,d[van];
int main(){
    read(n),read(u);
    for(i=1;i<=n;++i) read(d[i]);
    double maxn=-1;
    for(int i=1;i<n-1;++i){
        if(d[i+2]-d[i]>u) continue;
        int sum=d[upper_bound(d+1,d+n+1,d[i]+u)-d-1];
        maxn=max(1.0*(sum-d[i+1])/(sum-d[i]),maxn);
    }
    if(maxn<=0) return puts("-1"),0;
    printf("%.10f",maxn);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/jiaangk/p/9203747.html