L2-1 堆宝塔分数

发布于:2024-04-16 ⋅ 阅读:(171) ⋅ 点赞:(0)

本题链接: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;
}

最后提交:


网站公告

今日签到

点亮在社区的每一天
去签到