B. Sequence Game


B. Sequence Game

原创 于 2023-09-04 21:15:43 发布 · 粉丝可见 · 190 阅读 · 0 · 0 · 本内容遵循CC 4.0 BY-SA版权协议 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 GEO检测 · 编辑
文章链接:https://blog.csdn.net/hacker_51/article/details/132677662

题目:

样例:

cobol<br/>6<br/>3<br/>4 6 3<br/>3<br/>1 2 3<br/>5<br/>1 7 9 5 7<br/>1<br/>144<br/>2<br/>1 1<br/>5<br/>1 2 2 1 1<br/>
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] 还要小的值。

代码详解如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#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;
}

最后提交:


觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭

微信二维码

wechat pay

支付宝二维码

ali pay

B. Sequence Game
http://blog.angindem.cn/2023/09/04/Angindem-CSDN博客/051_51/
作者
Angindem
发布于
2023年9月4日
许可协议