F. Omkar and Landslide
F. Omkar and Landslide
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Omkar is standing at the foot of Celeste mountain. The summit is 𝑛 meters away from him, and he can see all of the mountains up to the summit, so for all 1≤𝑗≤𝑛 he knows that the height of the mountain at the point 𝑗 meters away from himself is ℎ𝑗 meters. It turns out that for all 𝑗 satisfying 1≤𝑗≤𝑛−1, ℎ𝑗<ℎ𝑗+1 (meaning that heights are strictly increasing).
Suddenly, a landslide occurs! While the landslide is occurring, the following occurs: every minute, if ℎ𝑗+2≤ℎ𝑗+1, then one square meter of dirt will slide from position 𝑗+1 to position 𝑗, so that ℎ𝑗+1 is decreased by 1 and ℎ𝑗 is increased by 1. These changes occur simultaneously, so for example, if ℎ𝑗+2≤ℎ𝑗+1 and ℎ𝑗+1+2≤ℎ𝑗+2 for some 𝑗, then ℎ𝑗 will be increased by 1, ℎ𝑗+2 will be decreased by 1, and ℎ𝑗+1 will be both increased and decreased by 1, meaning that in effect ℎ𝑗+1 is unchanged during that minute.
The landslide ends when there is no 𝑗 such that ℎ𝑗+2≤ℎ𝑗+1. Help Omkar figure out what the values of ℎ1,…,ℎ𝑛 will be after the landslide ends. It can be proven that under the given constraints, the landslide will always end in finitely many minutes.
Note that because of the large amount of input, it is recommended that your code uses fast IO.
Input
The first line contains a single integer 𝑛 (1≤𝑛≤106).
The second line contains 𝑛 integers ℎ1,ℎ2,…,ℎ𝑛 satisfying 0≤ℎ1<ℎ2<⋯<ℎ𝑛≤1012 — the heights.
Output
Output 𝑛 integers, where the 𝑗-th integer is the value of ℎ𝑗 after the landslide has stopped.
Example
inputCopy
4
2 6 7 8
outputCopy
5 5 6 7
Note
Initially, the mountain has heights 2,6,7,8.
In the first minute, we have 2+2≤6, so 2 increases to 3 and 6 decreases to 5, leaving 3,5,7,8.
In the second minute, we have 3+2≤5 and 5+2≤7, so 3 increases to 4, 5 is unchanged, and 7 decreases to 6, leaving 4,5,6,8.
In the third minute, we have 6+2≤8, so 6 increases to 7 and 8 decreases to 7, leaving 4,5,7,7.
In the fourth minute, we have 5+2≤7, so 5 increases to 6 and 7 decreases to 6, leaving 4,6,6,7.
In the fifth minute, we have 4+2≤6, so 4 increases to 5 and 6 decreases to 5, leaving 5,5,6,7.
In the sixth minute, nothing else can change so the landslide stops and our answer is 5,5,6,7.
题意分析:
题目大意
就大概是,一堆小土堆,要求如下:
- 严格保证
Hi<H(i+1)
- 若
H(i+1)-Hi > 1
,H(i+1)塌方到Hi,然后前者-1,后者+1。 - 直到不再变化为止。
思路部分
因为以下条件:
- 土堆高度一开始没有相等的。
- 所以最多只存在一组
H(i+1)=Hi
,这一点是好证明的。 - 而且相等的那组值在左移。
证明过程如下:
由于保证单调递增,所以说,只要有一个位置发生+1的变化,那么这个影响会一直传递到1。
直到a[1]=a[2],此时,若再次发生变化会使a[2]++。
那么可以发现最后的序列为一个公差为1的等差数列,但是其中允许有一对相同的数字。
那么求和+直接模拟即可。
最后的a[i]=i-1+(s-n*(n-1)/2)/n+(s-n*(n-1)/2)%n<=i;
也就是将每个元素先变成i-1,此时保证整个序列单调递增。
然后求和后再平均分配,最后补齐。
代码:
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <iomanip>
#include <cmath>
#include <map>
#include <stack>
#include <algorithm>
#include <queue>
#include <set>
#include <cstdio>
const int N = 1e6 + 7;
typedef long long ll;
using namespace std;
ll temp;
int n;
ll sum;
ll s[N], trans[N];
//要开long long啊,因为这个又WA了好几发
//毕竟1e12,我爬
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
sum = 0;
cin >> n;
if (n == 1) {
cin >> temp;
cout << temp << endl;
} else {
for (int i = 0; i < n; i++) {
cin >> s[i];
sum += s[i] - s[0] - i;
trans[i] = s[0] + i;
}
for (int i = 0; i < n; i++) {
trans[i] += sum / n;
}
for (int i = 0; i < sum % n; i++) {
trans[i]++;
}
for (int i = 0; i < n - 1; i++) {
cout << trans[i] << " ";
}
cout << trans[n - 1] << endl;
}
}