返回顶部

Codeforces Round #547 (Div. 3) C. Polycarp Restores Permutation (数学)

  • 题意:有一长度为\(n\)的序列\(p\),现在给你\(q_i=p_{i+1}-q_i \ (1\le i\le n)\),问你是否能还原出原序列,如果能救输出原序列,否则输出\(-1\).

  • 题解:由:\(q_i=p_{i+1}-p_i\),我们对其求前缀和可得:\(s_i=p_{i+1}-p_1\),然后再求出:\(\sum^{n-1}_{i=1}s_i=\sum^{n}_{i=2}p_i-(n-1)*p_1\),\(\sum^{n}_{i=2}p_i=\sum^{n}_{i=1}i-p_1=\frac{(n)*(n+1)}{2}-p_1\),所以:\(\sum^{n-1}_{i=1}s_i= \frac{(n)*(n+1)}{2}-n*p_1\),直接推出\(p_1\),然后线性求出所有\(p_i\)再判断一下就好了.

  • 代码:

    ll n;
    ll q[N],p[N];
    ll c[N];
    map<ll,int> mp;
    
    int main() {
        ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
        cin>>n;
        rep(i,1,n-1) cin>>q[i];
    
        rep(i,1,n-1) c[i]=c[i-1]+q[i];
    
        ll cnt=0;
        rep(i,1,n-1) cnt+=c[i];
        ll sum=n*(n+1)/2;
        sum-=cnt;
        if(sum%n!=0){
        	cout<<-1<<'\n';
        	return 0;
        }
        p[1]=sum/n;
    
        rep(i,1,n-1){
        	p[i+1]=p[i]+q[i];
        }
        bool flag=true;
        rep(i,1,n){
        	if(mp[p[i]]) {flag=false;break;}
        	if(p[i]<=0 || p[i]>n) {flag=false;break;}
        	mp[p[i]]++;
        }
        if(!flag) cout<<-1<<'\n';
        else{
        	rep(i,1,n) cout<<p[i]<<' ';
        }
    
        return 0;
    }
    
posted @ 2020-11-10 00:40  Rayotaku  阅读(76)  评论(0编辑  收藏  举报