Codeforces 957C二分

本文介绍了一个CodeForces平台上的题目解析,该题目涉及能量转换效率的计算及最大值寻找算法。通过分析不同状态的能量值,使用三水平激光方案进行能量转换,并在特定约束条件下寻找最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://codeforces.com/problemset/problem/957/C
C. Three-level Laser
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

An atom of element X can exist in n distinct states with energies E1 < E2 < ... < En. Arkady wants to build a laser on this element, using a three-level scheme. Here is a simplified description of the scheme.

Three distinct states i, j and k are selected, where i < j < k. After that the following process happens:

  1. initially the atom is in the state i,
  2. we spend Ek - Ei energy to put the atom in the state k,
  3. the atom emits a photon with useful energy Ek - Ej and changes its state to the state j,
  4. the atom spontaneously changes its state to the state i, losing energy Ej - Ei,
  5. the process repeats from step 1.

Let's define the energy conversion efficiency as , i. e. the ration between the useful energy of the photon and spent energy.

Due to some limitations, Arkady can only choose such three states that Ek - Ei ≤ U.

Help Arkady to find such the maximum possible energy conversion efficiency within the above constraints.

Input

The first line contains two integers n and U (3 ≤ n ≤ 105, 1 ≤ U ≤ 109) — the number of states and the maximum possible difference between Ek and Ei.

The second line contains a sequence of integers E1, E2, ..., En (1 ≤ E1 < E2... < En ≤ 109). It is guaranteed that all Ei are given in increasing order.

Output

If it is not possible to choose three states that satisfy all constraints, print -1.

Otherwise, print one real number η — the maximum possible energy conversion efficiency. Your answer is considered correct its absolute or relative error does not exceed 10 - 9.

Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .

Examples
Input
Copy
4 4
1 3 5 7
Output
0.5
Input
Copy
10 8
10 13 15 16 17 19 20 22 24 25
Output
0.875
Input
Copy
3 1
2 5 10
Output
-1
Note

In the first example choose states 1, 2 and 3, so that the energy conversion efficiency becomes equal to .

In the second example choose states 4, 5 and 9, so that the energy conversion efficiency becomes equal to .


题解:首先一定是相邻2个I,J数,然后尽可能使K最大化,所以可以N*LOGN,当然也有O(N)的做法,逆过来写,维护个不断减少的右指针。注意二分时候要upper_bound,然后-1。因为如果正好存在EJ+U的话,返回的是下一个,然后-1变成EJ+U。

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;

#define x0 x0___
#define y0 y0___
#define pb push_back
#define SZ(X) ((int)X.size())
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define ALL(X) X.begin(),X.end()
#define RALL(X) X.rbegin(),X.rend()
#define rep(i,j,k) for(int i = j;i <= k;i ++)
#define per(i,j,k) for(int i = j;i >= k;i --)
#define mem(a,p) memset(a,p,sizeof(a))


const ll MOD = 1E9 + 7;
ll qmod(ll a,ll b,ll c) {ll res=1;a%=c; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%c;a=a*a%c;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}

template<typename T, typename S>
void upmax(T& a,S b){if(a<b) a=b;}
template<typename T, typename S>
void upmin(T& a,S b){if(a>b) a=b;}
template<typename T>
void W(T b){cout << b << endl;}
void gettle() {while(1);}
void getre() {int t=0;t/=t;}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



const int N = 1E5 + 7;
int a[N];
int main()
{
    int n, U;
    scanf("%d %d", &n, &U);
    rep(i,1,n) scanf("%d",&a[i]);
    double res = -1.0;
    rep (i,1,n-2) {
        int t1 = a[i];
        int t2 = a[i+1];
        int up = U + t1;
        int k = upper_bound(a+1, a+1+n, up) - a - 1;
        if(k < i + 2) continue;
        upmax(res, (a[k]-t2*1.0)/(a[k]-t1));
    }
    return !printf("%.15f\n", res);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值