2019ACM-ICPC Shenyang network game -F-Honk's pool (multiset simulation, sign)

Honk's pool

1000ms

262144K

 

As we all know, Honk has nn pools, numbered as 11 ~ nn . There is a_iai liters water in the ii-th pool. Every day, Honk will perform the following operations in sequence.

  1. Find the pool with the most water (If there are more than one, choose one at random) and take one liter of water.

  2. Find the pool with the least water (If there are more than one, choose one at random) and pour one liter of water into the pool.

  3. Go home and rest (Waiting for the next day).

Please calculate the difference between the amount of water in the pool with the most water and the amount of water in the pool with the least water after the kk days.

Input

The input consists of multiple test cases. The input is terminated by the end of file.The number of data sets will not exceed 40

The first line of each test case contains two integers nn and kk, which indicate the number of pools and the number of days to operate the pool.

The second line of each test case contains nn integers, and the ii-th number represent a_iaiindicating the initial amount of water in the ii-th pool.

1 \ k \ a 500000 1 n 5 0 0 0 0 0,  1 \ k \ 10 ^ 9 1 k 1 0 1 \ The a_i \ 10 ^ 9 1 of at 1 0 9.

Output

For each test case, print one line containing the answer described above.

Sample input 1

4 100
1 1 10 10

Sample output 1

1

Sample input 2

4 3
2 2 2 2

Sample output 2

0

Simulation is not even TLE, the data subject is too water it. . . . . . .

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 multiset<ll>ste;
 5 int main()
 6 {
 7     int n,k;
 8     while(~scanf("%d%lld",&n,&k))
 9     {
10         ste.clear();
11         for(int i=1;i<=n;++i)
12         {
13             ll a;
14             scanf("%lld",&a);
15             ste.insert(a);
16         }
17         multiset<ll>::iterator it1,it2;
18         while(k--)
19         {
20             it1=ste.begin();
21             ll a=*it1;
22             ste.erase(it1);
23             ste.insert(++a);
24 
25             it2=--ste.end();
26             a=*it2;
27             ste.erase(it2);
28             ste.insert(--a);
29         }
30         ll ans=(*(--ste.end()))-(*ste.begin());
31         printf("%lld\n",ans);
32     }
33     return 0;
34 }

 

Guess you like

Origin www.cnblogs.com/CharlieWade/p/11519899.html