题意:
给定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;
}
本文深入探讨了一种构造特定非递减序列的算法,通过给定的一组数值,将其拆分并重新排列,确保最终序列满足非递减条件。文章详细介绍了算法的实现思路和步骤,包括如何判断和调整序列元素,以及使用C++代码实现这一过程。
121

被折叠的 条评论
为什么被折叠?



