Blue Bridge Cup Daily Question 2023.9.28

AcWing 4409. Cut Bamboo - AcWing

Question description

Question analysis 

Note: The range of sqrtl is long double, which is more accurate than sqrt

Use the priority queue to maintain an interval. If the consecutive sections are the same, they will be merged into one interval. Enumerate from large to small, and take out the largest section first each time. Sort by double keywords. The first keyword is v, and the second keyword is At the left endpoint of the interval, first take out the top element of the heap. After taking it out, merge the elements that can be merged together. After the merge is completed, just do one operation.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
ll n, res, a[N];
struct node
{
	ll l, r, h;
	bool operator< (const node& x) const
	{
		if(h != x.h)return x.h > h;
		return x.l < l;
	}
};
ll f(ll x)
{
	return sqrtl(x / 2 + 1);
}
priority_queue<node> q;
int main()
{
	cin >> n;
	for(int i = 0; i < n; i ++)cin >> a[i];
	//先将高度相同的一段加入队列 
	for(int i = 0; i < n; i ++)
	{
		int j = i + 1;
		while(j < n && a[i] == a[j])j ++;
		q.push({i, j - 1, a[i]});
		i = j - 1;
	}
	//进行合并操作
	while(q.top().h > 1)
	{
		auto t = q.top();
		q.pop();
		while(q.size() && q.top().h == t.h && t.r + 1 == q.top().l)//如果堆非空并且现高度与相邻高度一致(相邻即t.r + 1 == q.top().l) 
		{
			t.r = q.top().r;
			q.pop();
		}
		q.push({t.l, t.r, f(t.h)});
		if(t.h > 1)res ++;
	} 
	cout << res << '\n';
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_75087931/article/details/133385304
Recommended