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.

样例输入1
4 100
1 1 10 10
样例输出1
1
样例输入2
4 3
2 2 2 2
样例输出2
0
题意:给n个数数、每天对它进行如下顺序操作:1、给最大值减一 2、给最小值加一
问k天之后最大值和最小值的差值!
题目链接:https://nanti.jisuanke.com/t/41406
solution:比较坑爹的题目、坑点比较多、琢磨了不少时间、不能单纯的模拟,妥妥的超时了
分析:咱们记录最大值和最小值、每天的操作后最值只会有三种变化情况,
记最小值min、最大值max、
则min变化为min、min + 1、max - 1最小的一个、
同理max变化为max、max - 1、min + 1最大的一个、
如此模拟、可得答案

#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;
}
发布了161 篇原创文章 · 获赞 9 · 访问量 5246
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 像素格子 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览