题解 CF1392F 【Omkar and Landslide】

· · 题解

题意

给你一个严格递增的数列,定义一次操作为同时对于所有 i<n 且 a_{i+1}-a_i\geq2 的 a_i 增加 1 ,a_{i+1} 减小 1 。

问你一直操作直到没有任何数能在操作中改变后,最终所有数的大小。

思路

Tag:玄学,找规律,数学

我们先手膜一些数据。

\{1,3,5,7\}\to\{3,4,4,5\} \{1,2,3,10\}\to\{3,4,4,5\} \{1,4,5,6\}\to\{3,4,4,5\} \{2,3,4,7\}\to\{3,4,4,5\} \{2,3,4,6\}\to\{3,3,4,5\}

发现了什么?最终的排列只和所有数的和有关。

我们再手膜一些数据。

\sum=16\to\{3,3,4,5\}=\{2+1,3,4,5\} \sum=17\to\{3,4,4,5\}=\{2+1,3+1,4,5\} \sum=18\to\{3,4,5,5\}=\{2+1,3+1,4+1,5\} \sum=19\to\{3,4,5,6\}=\{3,4,5,6\}

发现了什么?简单的来说,答案开始为 \{1,2,3,\cdots,n\} ,然后在总和不超过 \sum a_i 的前提下依次往每一位增加 1。

现在,问题变得很 simple 了:给定 n 和 a ,构造答案,只需要一点点 Math 知识和决心就可以过题了!

代码


#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}
signed main()
{
    int n=read(),sum=0;
    for(int i=1; i<=n; i++) sum+=read();
    int x=(((sum<<1)/n-n+1)>>1),y=sum-((((x<<1)+n-1)*n)>>1);
    for(int i=1; i<=n; i++) if(i<=y) printf("%lld ",i+x); else printf("%lld ",i+x-1); 
    return 0;
}