L2-1 堆宝塔

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

题目:

样例:

输入
11
10 8 9 5 12 11 4 3 1 9 15
输出
4 5

思路:

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

代码详解如下:

#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;
}

最后提交:

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值