题意:
给定n/2个数,每个数拆分成两个数,将这两个数分别置于首尾(次首、次尾逐渐内缩)要求排列后这个序列是非递减的,要求你输出这个序列。
思路:
率先想到0 + a[i],判断首尾是否满足条件,满足就插入,不满足就内缩。
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL maxn = 1e5+5;
LL a[maxn];
vector<LL> v;
int main()
{
int n;
cin >> n;
for (int i = 0; i < n / 2; i++) cin >> a[i];
v.push_back(0);
v.push_back(a[0]);
LL ps = 0, pe = a[0];
for (int i = 1; i < n / 2; i++)
{
if (a[i] > pe)
{
LL pps = a[i] - pe;
LL ppe = pe;
if (pps < ps)
{
ppe -= (ps - pps);
v.push_back(ps);
v.push_back(ppe);
pe = ppe;
}
else
{
v.push_back(pe);
v.push_back(a[i] - pe);
ps = a[i] - pe;
}
}
else
{
if (0 < ps)
{
v.push_back(ps);
v.push_back(a[i] - ps);
pe = a[i] - ps;
}
else
{
v.push_back(0);
v.push_back(a[i]);
pe = a[i];
}
}
}
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); i++)
{
if (i) cout << " ";
cout << v[i];
}
cout << endl;
return 0;
}