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+an−i+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} ri≤ri−1且 l i ≥ l i − 1 l_i≥l_{i-1} li≥li−1即可。