题目:
样例:
|
6 4 3 2 6 3 3 3 1 2 3 6 1 7 9 3 5 7 1 144 2 1 1 6 1 2 2 1 1 1 |
思路:
找规律,思维题。我们查看 a 和 b 之间的关系可以知道,a[i - 1] <= a[i] 就放进 b 序列当中,遇到 a[i - 1] > a[i] 的时候我们要插入一个比 a[i] 还要小的值。
代码详解如下:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <unordered_map>
#define endl '\n'
#define YES puts("YES")
#define NO puts("NO")
#define umap unordered_map
#pragma GCC optimize(3,"Ofast","inline")
#define ___G std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
using namespace std;
const int N = 2e6 + 10;
inline void solve()
{
int x; // 读取值
vector<int>ans; // 答案 a 数组
int n; // b 数组大小
cin >> n;
cin >> x; // 读入 a1
ans.emplace_back(x); // 首先存储 a1;
int r = x; // r 作为记录前一个数值
for(int i = 1;i < n;++i)
{
cin >> x;
// 如果 b 数组中当前元素大于上一个记录的元素,那么存储好 a 数组里面
if(x >= r) ans.emplace_back(x);
else
{
ans.emplace_back(1);
ans.emplace_back(x);
}
r = x; // 记录当前元素
}
// 输出答案
cout << ans.size() << endl;
for(auto i : ans)
{
cout << i << ' ';
}
cout << endl;
}
int main()
{
// freopen("a.txt", "r", stdin);
___G;
int _t = 1;
cin >> _t;
while (_t--)
{
solve();
}
return 0;
}