Codeforces 758A. Holiday Of Equality 贪心

题目大意:

给定一个长为(n)序列,每次操作在一个数上+1,求最小的操作次数使所有的数大小相同.

题解:

对这种题无话可说

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
	x=0;char ch;bool flag = false;
	while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
	while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = 128;
int a[maxn];
int main(){
	int n;read(n);
	int maxx = 0;
	for(int i=1;i<=n;++i){
		read(a[i]);
		maxx = cat_max(maxx,a[i]);
	}
	int ans = 0;
	for(int i=1;i<=n;++i){
		ans += maxx - a[i];
	}printf("%d",ans);
	getchar();getchar();
	return 0;
}

拓展:

如果一次操作既可以+1也可以-1.会是什么样.

解答:

一个结论,最终一定都变为中位数。

原文地址:https://www.cnblogs.com/Skyminer/p/6357659.html