The Preliminary Contest for ICPC Asia Shenyang 2019 F. Honk's pool

Topic links: https://nanti.jisuanke.com/t/41406

Thinking: If k is large enough for several days, then all will tend to sink two situations:

① All pools are the same level, that is, the average water level

Pool and pool ② lowest water level of the highest water level height difference of only a height, and the lowest water level must be the average water level

If k gave limitation:

Of course, we need to calculate the average of all the pool height.

Then sorted from low to high, the water level is less than half the average, two points above the mean water level,

Then determines whether the expected value of half the number of days required less than equal to k. Then half to find the lowest maximum water level,

The minimum value of the highest level, both of subtraction is the answer.


 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cstdio>
 5 #include <map>
 6 #define rep(i,j,k) for(int i = (j); i <= (k); ++i)
 7 #define per(i,j,k) for(int i = (j); i >= (k); --i)
 8 #define rep__(i,j,k) for(int i = (j); i < (k); ++i)
 9 #define per__(i,j,k) for(int i = (j); i > (k); --i)
10 #define inf 1e9
11 using namespace std;
12 typedef long long LL;
13 const int N = 500010;
14 
15 int arr[N],k,n;
16 LL sum,ave;
17 
18 bool check(int mid,int choice){
19     int sum = 0;
20     if(choice == 1){ //大于平均值的
21         rep(i,1,n){
22             if(arr[i] >= mid) break;
23             sum += (mid - arr[i]);
24         }
25     }
26 is      the else { // less than the average of 
27          per (I, n-, . 1 ) {
 28              IF (ARR [I] <= MID) BREAK ;
 29              SUM + = (ARR [I] - MID);
 30          }
 31 is      }
 32  
33 is      IF (SUM <= K) return  to true ;
 34 is      the else  return  to false ;
 35  }
 36  
37 [  int main () {
 38 is  
39      the while (~ Scanf ( " % D " &, n-)) {
 40         SUM = 0 ;
 41 is          Scanf ( " % D " , & K);
 42 is          REP (I, . 1 , n-) {
 43 is              Scanf ( " % D " , ARR + I);
 44 is              + = SUM ARR [I];
 45          }
 46 is          
47          Sort (ARR + . 1 , ARR + . 1 + n-);
 48          Ave = SUM / n-;
 49  
50          int l_end, r_start; // minimum water level below the maximum level of average, above average 
51         = l_end Ave;
 52 is          SUM n-% == 0 r_start = Ave:? r_start = Ave + . 1 ; // Can bisector 
53 is          
54 is          int ans_1, ans_2;
 55          int L = ARR [ . 1 ]; // left boundary 
56 is          int R & lt l_end =; // right boundary 
57 is          int MID;
 58          the while (L <= R & lt) {
 59              MID = (L + R & lt) >> . 1 ;
 60              IF (Check (MID, . 1 )) {
 61 is                  ans_1 = MID;
62 is                  L = MID + . 1 ;
 63 is              }
 64              the else R & lt MID = - . 1 ;
 65          }
 66  
67          L = r_start; // left boundary 
68          R & lt = [n-] ARR; // right boundary 
69          the while (L <= R & lt) {
 70              MID = (L + R & lt) >> . 1 ;
 71 is              IF (Check (MID, 2 )) {
 72                  ans_2 = MID;
 73 is                  R & lt MID = - . 1 ;
 74             }
75             else l = mid + 1;
76         }
77 
78         printf("%d\n",ans_2 - ans_1);
79     }
80 
81     getchar();getchar();
82     return 0;
83 }

 

Guess you like

Origin www.cnblogs.com/SSummerZzz/p/11580811.html