# 2019icpc 沈阳网络赛 F. Honk's pool（模拟）

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

Find the pool with the most water (If there are more than one, choose one at random) and take one liter of water.
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.
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 k 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 n and k, 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 i-th number represent a_i indicating the initial amount of water in the i-th pool.
1≤n≤500000, 1≤k≤10^9 ,1≤ai≤10^9.
Output
For each test case, print one line containing the answer described above.

4 100
1 1 10 10

1

4 3
2 2 2 2

0

solution:比较坑爹的题目、坑点比较多、琢磨了不少时间、不能单纯的模拟，妥妥的超时了

#include <bits/stdc++.h>
using namespace std;

int main()
{
int n, k, temp;
while (~scanf("%d %d", &n, &k)){
int mini = 1000000000, maxi = 0;
vector<int> arr(n);
unordered_map<int, int> cnt;
for (int i = 0; i < n; ++i){
scanf("%d", &arr[i]);
++cnt[arr[i]];
if (mini > arr[i])mini = arr[i];
if (maxi < arr[i])maxi = arr[i];
}
while (k--){
--cnt[maxi];
++cnt[maxi - 1];
if (maxi - 1 < mini)mini = maxi - 1;
if (cnt[maxi] > 0)maxi = maxi;
else --maxi;
--cnt[mini];
++cnt[mini + 1];
if (mini + 1 > maxi)maxi = mini + 1;
if (cnt[mini] > 0)mini = mini;
else mini++;
}
cout << maxi - mini << endl;
}
return 0;
} ©️2019 CSDN 皮肤主题: 像素格子 设计师: CSDN官方博客 