cf Educational Codeforces Round 56 C. Mishka and the Last Exam

C. Mishka and the Last Exam
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.

There were n classes of that subject during the semester and on i-th class professor mentioned some non-negative integer ai to the students. It turned out, the exam was to tell the whole sequence back to the professor.

Sounds easy enough for those who attended every class, doesn’t it?

Obviously Mishka didn’t attend any classes. However, professor left some clues on the values of a to help out students like Mishka:

a was sorted in non-decreasing order (a1≤a2≤⋯≤an);
n was even;
the following sequence b, consisting of n2 elements, was formed and given out to students: bi=ai+an−i+1.
Professor also mentioned that any sequence a, which produces sequence b with the presented technique, will be acceptable.

Help Mishka to pass that last exam. Restore any sorted sequence a of non-negative integers, which produces sequence b with the presented technique. It is guaranteed that there exists at least one correct sequence a, which produces the given sequence b.

Input
The first line contains a single integer n (2≤n≤2⋅10^5) — the length of sequence a. n is always even.

The second line contains n2 integers b1,b2,…,bn2 (0≤bi≤10^18) — sequence b, where bi=ai+an−i+1.

It is guaranteed that there exists at least one correct sequence a, which produces the given sequence b.

Output
Print n integers a1,a2,…,an (0≤ai≤10^18) in a single line.

a1≤a2≤⋯≤an should be satisfied.

bi=ai+an−i+1 should be satisfied for all valid i.

Examples
input
4
5 6
output
2 3 3 3
input
6
2 1 2
output
0 0 1 1 1 2

中文:

给你一个整数n,n一定是偶数。然后给你n/2个数 a i a_i ai,让你将这n/2个数按照下面的规则,拆分成n个数 b i b_i bi,这n个数按照非递减顺序排列。拆分规则为 b i = a i + a n − i + 1 b_i=a_i+a_{n-i+1} bi=ai+ani+1,给定的 a i a_i ai保证有解

代码:

#include<bits/stdc++.h>
using namespace std;
#define make_pair mp

const int maxn=1e5+1;
typedef long long ll;
typedef pair<int,int> pii;

ll n;
ll b;
ll l[maxn],r[maxn];
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n)
    {
        memset(l,0,sizeof(l));
        memset(r,0,sizeof(r));

        for(int i=1;i<=n/2;i++)
        {
            cin>>b;
            if(i==1)
            {
                l[i]=0;
                r[i]=b;
            }
            else
            {
                if(b<=r[i-1]&&l[i-1]==0)
                {
                    r[i]=b;
                    l[i]=0;
                }
                else
                {
                    if(b-r[i-1]>=l[i-1])
                    {
                        r[i]=r[i-1];
                        l[i]=b-r[i-1];
                    }
                    else
                    {
                        l[i]=l[i-1];
                        r[i]=b-l[i];
                    }
                }
            }
        }
        for(int i=1;i<=n/2;i++)
            cout<<l[i]<<" ";

        for(int i=n/2;i>=1;i--)
        {
            if(i!=1)
                cout<<r[i]<<" ";
            else
                cout<<r[i]<<endl;
        }

    }
    return 0;
}

思路:

a i a_i ai拆分成两个数 l i l_i li r i r_i ri,分别放在左侧和右侧。那么每次保证 r i r_i ri尽量的大, l i l_i li尽量的小。同时要满足 r i ≤ r i − 1 r_i≤r_{i-1} riri1 l i ≥ l i − 1 l_i≥l_{i-1} lili1即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值