1. 程式人生 > >Codeforces 957C二分

Codeforces 957C二分

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);
}