L2-1 堆宝塔


L2-1 堆宝塔

原创 已于 2024-04-17 21:22:30 修改 · 粉丝可见 · 344 阅读 · 1 · 3 · 本内容遵循CC 4.0 BY-SA版权协议 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 GEO检测 · 编辑
文章链接:https://blog.csdn.net/hacker_51/article/details/137834484

本题链接: PTA | 程序设计类实验辅助教学平台

题目:

样例:

cobol<br/>11<br/>10 8 9 5 12 11 4 3 1 9 15<br/>
4 5

思路:

这是一道模拟题,需要有耐心读题,跟着题目走一遍,就可以了。

代码详解如下:

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
68
69
70
71
72
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#define endl '\n'
#define int long long
#define YES puts("YES")
#define NO puts("NO")
#define umap unordered_map
#define All(x) x.begin(),x.end()
#pragma GCC optimize(3,"Ofast","inline")
#define IOS std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
using namespace std;
const int N = 2e6 + 10;
inline void solve();

signed main()
{
// freopen("a.txt", "r", stdin);
IOS;
int _t = 1;
// cin >> _t;
while (_t--)
{
solve();
}
return 0;
}
int n;
vector<int>a,b;
inline void solve()
{
int sum = 0;
int maxs = 0;

cin >> n;
int x;
cin >> x;
a.emplace_back(x); // 先拿第一个当底盘

for(int i = 1;i < n;++i)
{
cin >> x;

if(x < a.back()) a.emplace_back(x);
else
{
if(b.size())
{
if(x > b.back()) b.emplace_back(x);
else
{
++sum;
maxs = max(maxs,(int)a.size());
a.clear();
while(b.size() and b.back() > x)
{
a.emplace_back(b.back());
b.pop_back();
}
a.emplace_back(x);
}
}else b.emplace_back(x);
}
}
maxs = max(maxs,(int)a.size());
if(a.size()) ++sum;
if(b.size()) ++sum;
cout << sum << ' ' << maxs << endl;
}

最后提交:


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

微信二维码

wechat pay

支付宝二维码

ali pay

L2-1 堆宝塔
http://blog.angindem.cn/2024/04/17/Angindem-CSDN博客/148_148/
作者
Angindem
发布于
2024年4月17日
许可协议