2019ACM-ICPC沈阳网络赛-F-Honk's pool(multiset模拟、签到)

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.

扫描二维码关注公众号,回复: 7280445 查看本文章

1 \le n \le 5000001n500000, 1 \le k \le 10^91k109, 1 \le a_i \le 10^91ai109.

Output

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

样例输入1

4 100
1 1 10 10

样例输出1

1

样例输入2

4 3
2 2 2 2

样例输出2

0

模拟竟然没有TLE,题目数据也太水了吧。。。。。。。

 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 }

猜你喜欢

转载自www.cnblogs.com/CharlieWade/p/11519899.html