【题解】Codeforces Global Round 10 -- F

题目:https://codeforces.ml/contest/1392/problem/F

题解

以下是比较投机取巧的方法!
先打个暴力找下规律。

#include 
using namespace std;

int a[1000010], c[1000010], n;
bool flag;

int main(){
     
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	while (!flag){
     
		flag = 1;
		for (int i  = 1; i <= n; i++)
			c[i] = 0;
		for (int i = 1; i < n; i++)
			if (a[i] - a[i+1] >= 2) c[i]--, c[i+1]++, flag = 0;
			else if (a[i+1] - a[i] >= 2) c[i]++, c[i+1]--, flag = 0;
		for (int i = 1; i <= n; i++)
			a[i] += c[i];		
	}
	for (int i = 1; i <= n; i++)
		cout << a[i] << " ";
	return 0;
}

规律还是挺明显的,只要输入数列各数字之和一定,输出的序列也一定。且输出序列一定为a, a+1, a+2, … , a+k, a+k, a+k+1,…,a+n-2。由此可以写出代码:

#include 
using namespace std;
typedef long long ll;

ll a[1000010], s, x, n;

int main(){
     
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i], s += a[i] - i;
	x = s / n;
	for (int i = 1; i <= n; i++)
		cout << x + i + (i <= s % n) << " ";
	return 0;
}

你可能感兴趣的:(算法)