CF补题计划_F. Omkar and Landslide_经典思维题^-^

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.

题意分析:

题目大意

就大概是,一堆小土堆,要求如下:

  1. 严格保证Hi<H(i+1)
  2. H(i+1)-Hi > 1,H(i+1)塌方到Hi,然后前者-1,后者+1。
  3. 直到不再变化为止。

思路部分

因为以下条件:

  • 土堆高度一开始没有相等的。
  • 所以最多只存在一组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;

    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值