Gifts "JSOI 2015"

Description [title]
gift shop, a total of N gifts in a row, each gift has its aesthetics. Ranked \ (i (1 \ leq i \ leq N) \) presents the appearance of the positions is a positive integer \ (A_I \) . JYY decide which elected a continuous period of that numbered gift \ (i, i + 1, ..., j-1, j \) gift. These selected gifts aesthetic appearance is defined as: \ ((M (I, J) -m (I, J)) / (I-J + K) \) , where \ (M (i, j) \) represents \ (max \ {A_i, A_. 1} + {I .... A_j \} \) , \ (m (I, J) \) represents \ (min \ {A_i, A_ {i + 1} ... .A_j \} \) , \ (K \) for a given positive integer.

Since the gas can not be too small, so JYY number of pieces selected gifts minimum \ (L \) pieces; at the same time, the election is not easy to get too much, so the gift up to the election \ (R \) pieces. How JYY should choose in order to get the maximum level of aesthetics? Because too many gifts Tiao, JYY going to put this question to you would be programming.

[Input Format]
this question multiple sets of data for each test point. Input of the first line contains a positive integer \ (T (T \ Leq 10) \) , expressed T set of data. Each set of data consists of two rows, the first row of four non-negative integer \ (N, K, L, R & lt (2 \ Leq L \ R & lt Leq \ N Leq) \) . The second line contains \ (N \) positive integers, respectively for \ (A_1, A_2 .... A_n \) , \ ((A_i \ ^ 10 Leq. 8) \) , \ (N, K \ Leq 50,000 \ ) .

[] Output format
output \ (T \) lines of a non-negative real number, successively each data corresponding to the answer, the answer data to ensure that no more than \ (10 ^ 3 \) . Output rounded to four decimal places.

[Thinking]
this question of violence is quite easy to write, direct enumeration of the number of gift items. . . Conscience title

Below is a positive solution:

Clearly superior emulated is a section taken some interval \ ([l, r] \ ) such that the interval maximum value and the minimum value of which is located a \ (A [L] \) , the other in \ (A [r] \) . Because if such emulated, the minimum and maximum values for each of the intervals, \ ((R & lt - L + K) \) be as small as possible.
Therefore, the discussion classification:
\ (1 \) interval size smaller than the limit \ (L \) must now be expanded to the size of the interval \ (L \) directly monotonically maintenance queue length \ (L \) minimum and maximum range, calculates \ (n-L + 1 \ ) intervals.
\ (2 \) binary answer \ (X \)
for an interval \ ([L, R & lt] \) :
If the maximum value \ (A [L] \) at a minimum value \ (A [R] \ ) at, there \ (A [L] -A [R & lt] - (R & lt-L + K) * X> 0 \) ; if \ (max ((A [i ] + i * mid) - (A [ j] + j * mid) -k * mid)> = 0 \) then \ (X \) may continue to expand.
If the maximum value\ (A [R] \) at a minimum value \ (A [L] \) at the calculated \ (max ((A [i ] -i * mid) - (A [j] -j * mid) -k * mid) \) is greater than 0.
The \ (k * mid \) moves to the right of the inequality, the remaining \ ((A [i] -i * mid) \) maximum may monotonically queue maintenance.
You can enumerate every point at the left end or right end point as a maximum and then monotonically by the scope of legal queue to find a minimum value is calculated.
This is done on the topicAs if still not clear

The Code

#include <bits/stdc++.h>
#define ri register int
using namespace std;
typedef long long ll;

ll t, n, k, l, r;
ll st1, st2, ed1, ed2;
ll que1[50005], que2[50005];
ll q[50005], head, tail;
ll a[50005];
double cur[50005];
double ans;

ll read() {
    ll ret = 0, flag = 1;
    char ch = getchar();
    while (ch > '9' || ch < '0') {
        if (ch == '-') flag = -1;
        ch = getchar(); 
    }
    while (ch <= '9' && ch >= '0') {
        ret = ret * 10 + ch - '0';
        ch = getchar();
    }
    return ret * flag;
}

bool check(double mid) {
    for (ri i = 1; i <= n; i++) {
        cur[i] = a[i] - mid * i;
    }
    head = 1; tail = 0;
    double nowans = -1e9;
    for (ri i = l + 1; i <= n; i++) {
        while (head <= tail && i - q[head] >= r) head++;
        while (head <= tail && cur[q[tail]] >= cur[i - l]) tail--;
        q[++tail] = i - l;
        nowans = max(nowans, cur[i] - cur[q[head]]);
    }
    for (ri i = 1; i <= n; i++) {
        cur[i] = a[i] + mid * i;
    }
    head = 1; tail = 0;
    for (ri i = n - l; i >= 1; i--) {
        while (head <= tail && q[head] - i >= r) head++;
        while (head <= tail && cur[q[tail]] >= cur[i + l]) tail--;
        q[++tail] = i + l;
        nowans = max(nowans, cur[i] - cur[q[head]]);
    }
    return nowans >= k * mid;
}

int main() {
    t = read();
    while (t--) {
        n = read(); k = read(); l = read(); r = read();
        for (ri i = 1; i <= n; i++) {
            a[i] = read();
        }
        st1 = st2 = 1;
        ed1 = ed2 = 0;
        for (ri i = 1; i < l; i++) {
            while (st1 <= ed1 && a[que1[ed1]] >= a[i]) ed1--;
            while (st2 <= ed2 && a[que2[ed2]] <= a[i]) ed2--;
            que1[++ed1] = que2[++ed2] = i;
        }
        ans = -1e9;
        for (ri i = l; i <= n; i++) {
            while (st1 <= ed1 && i - que1[st1] >= l) st1++;
            while (st2 <= ed2 && i - que2[st2] >= l) st2++;
            while (st1 <= ed1 && a[que1[ed1]] >= a[i]) ed1--;
            while (st2 <= ed2 && a[que2[ed2]] <= a[i]) ed2--;
            que1[++ed1] = que2[++ed2] = i;
            ans = max(ans, 1.0 * (a[que2[st2]] - a[que1[st1]]) / (l + k - 1));
        }
        double l = 0, r = 1005, mid;
        while (r - l >= 1e-7) {
            mid = (l + r) / 2;
            if (check(mid)) {
                l = mid + 0.000001;
                ans = max(ans, mid);
            } else r = mid - 0.000001;
        }
        printf("%.4lf\n", ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/ak-dream/p/AK_DREAM3.html