codeforce-1201-C explanations

Title: give you an array containing integers A n (n is odd), made of A k times the following operations:

  • Sorting an array so that the array are arranged in a non-descending order.
  • Selecting the median of the array, and then add a

And ultimately makes the median array maximum.

Input: The first line of the input two numbers n and k - array size and number of operations performed

Output: Output largest median

Chestnut: Input 32

       1 3 5

   Output 5

(For that matter I would like to be the beginning of half time but the complexity is O (kn) burst so change train of thought by half by value array)

Ideas:  the maximum value, then the end of the array should satisfy calculating - starting from the middle to the value of i is equal to the intermediate position and a median equal to the maximum.

    First, the definition of a right value (R value should be greater than each element in the array, as long as greater than a given title can 1e9), wherein the number of bits is obtained. Second, determine the number of bits of which meets the definition of the maximum of the median, and if so, then the left median value for this (asking for is the largest median). If not, then the right for this median value minus a

 

check function is judged whether definition of the maximum median

BOOL Check (LL mid) // Check whether the true mid median 
{ 
    LL S = 0 ;
     for ( int I = n-/ 2 ; I <n-; I ++ ) {
         IF (mid> A [I])   // A [i]> mid a [i ] is not the carry, since it is not added a [i] who 
            S = S + MID - a [i]; 
    } 
    return (S <= K); // if s> when k, to be a number of mid to require more than the number, the less mid plus man so this mid 
}                    //   not a big occasion of the mid point it may be small if the value is in the

Then narrow the range to find the maximum value

the while (L < R & lt) { 
        mid = (L + R & lt + . 1 ) / 2 ;
         IF (Check (mid))   // if mid is the median time to continue to be evaluated and selected is greater than the mid value is full 
            l = mid;
         the else 
            R & lt = mid - . 1 ;   // mid value is not too great when the mid described, to tune to the small 
    }

Why mid = (l + r + 1) / 2? Because a binary search is generally rounded down

For details, see: https://www.cnblogs.com/flipped/p/4991658.html

Complete code:

#include <the iostream> 
#include <algorithm>
 the using  namespace STD; 

typedef Long  Long LL;
 const  int MAXN = 200010 ; 
LL n-, K, A [MAXN]; 

BOOL Check (LL mid) // Check whether it is true in the mid value 
{ 
    LL S = 0 ;
     for ( int I = n-/ 2 ; I <n-; I ++ ) {
         IF (MID> a [I])   // a [I]> MID a [I] is not the carry bit, because to add less than A [i] who 
            S + MID = - A [i]; 
    } 
    return (S <= K);// When s> when k, to be a number of mid to require more than the number, the less mid plus man so this mid 
}                  //   not a big occasion of the mid point it may be small if the value is in the 

int main () 
{ 
    int I; 
    LL L = 0 , R & lt = 3E9; 
    LL MID; 
    scanf_s ( " % LLD% LLD " , & n-, & K);
     for (I = 0 ; I <n-; I ++ ) 
        scanf_s ( " LLD% " , & A [I]); 
    Sort (A, A + n-); 

    the while (L < R & lt) { 
        MID = (L + R & lt + . 1) / 2 ;
         IF (Check (mid))   // when it is time to the mid value may be selected and continues to find whether the value is greater than the full mid value 
            L = mid;
         the else 
            R & lt = mid - . 1 ;   // mid not a value when too much mid described, to adjust to small 
    } 
    the printf ( " % LLD " , L);
     return  0 ; 
}

I suggest running through the gradual plus method of calculating the understanding of this question.

 As the author Caishuxueqian, the text will inevitably there are some errors or omissions, I urge readers criticism.

  • Add to phrasebook
     
    • Without this set of words: -> Chinese (simplified) ...
       
    • Create a new set of words ...
  • copy

Guess you like

Origin www.cnblogs.com/panda-eat-meat/p/11617781.html