【CF1201C】Maximum Median

Meaning of the questions:

Given a length of n-$ $ sequence, and the opportunity to obtain $ k $ operations, and each operation is to add $ value wherein a number of $ 1.

Reasonable arrangements which seek $ k $ operations, so that the resulting sequence of median maximum.

$ 1 \ n \ * 2 5.1 10 ^ \ k \ 10 ^ 9 $

analysis:

We want to be greedy strategy, if the original sequence number to be less than the median of the operation, then the answer is certainly not the best, it is better to larger numbers.

So, we can all be deleted in this sequence number is less than the median.

After deleting that original median becomes the smallest number in the sequence that much.

The problem will be converted into the "minimum maximum" this kind of problem, and yes, you can half the answer.

We certainly have to operate to the smallest number possible, but this number once beyond the other number is no longer the smallest number.

Therefore, we must analyze the following figure as an example:

We want $ 1 $ dot as large as possible, then they would have to pay would point number one the number of operations, but at the same time to ensure that the numbers are not greater than $ 1 $ other number.

So, we will be highly (ie answer) divided by two:

If you want $ 1 to $ dot filling two layers, so you want to do:

Thus the need for $ 1 $ operations;

If you want $ 1 $ filled to three-point numbers, do this:

It needs a total of $ 1 + $ 3 = 4 operations.

Therefore, the test is to look at all the conditions to be determined is less than the height of the dot height difference whether add up to more than $ k. $

In this way, you can write code:

#include <algorithm>
#include <cstdio>
typedef long long ll;

ll n, k, a[200010], ans, l, r;

bool check(ll x){
    ll sum = 0;
    for(ll i=1; i<=n; i++)
        if(a[i] < x)
            sum += x - a[i];    //算高度差之和
        else
            break;
    return sum <= k;         //检验条件
}

int main(){

    scanf("%lld%lld", &n, &K);
     for (LL I = . 1 ; I <= n-; I ++ ) 
        Scanf ( " % LLD " , & A [I]); 
    STD :: Sort (A + . 1 , A + n-+ . 1 ); // Note bipartite monotonicity, thus to sort 

    n = n / 2 + . 1 ; // for convenience, where advance change n scale
     for (LL I = . 1 ; I <= n; I ++ ) 
        a [I] = a [n + I - . 1 ]; 

    L = . 1 , = R & lt 2E9;
     the while (L < R & lt) { 
        LL MID = (L + R & lt + . 1) >> 1;
        if(check(mid)) l = mid;
        else r = mid - 1;
    }

    printf("%lld\n", l);

    return 0;
}

Complexity $ \ text {O (n log n)}. $


In addition, this question there are other practices, such as greed (linear complexity), etc., are not explained here, are interested you can go look.

 

Guess you like

Origin www.cnblogs.com/zengpeichen/p/11515613.html