蓝桥杯每日一题2023.9.28

AcWing 4409. 砍竹子 - AcWing

题目描述

题目分析

注:sqrtl的范围为long double,比sqrt更加精确

使用优先队列维护一段区间,如果连续一段相同就合并为一个区间,从大到小去枚举,每次先取出最大的一段,双关键字排序,第一关键字是v,第二关键字是区间的左端点,先把堆顶元素取出,取完之后,能合并的合并在一起,合并完之后统一做一次操作即可

cpp 复制代码
#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;
}
相关推荐
政企项目老覃7 小时前
大模型幻觉治理与自动评测:金融风控场景的落地实践
人工智能·算法·机器学习
淡海水7 小时前
08-03-不可变-ImmutableDictionary-TKey-TValue-与ImmutableHashSet-T-持久化哈希树
数据结构·算法·c#·哈希算法·dictionary·immutable
hansang_IR7 小时前
【题解】[APIO2023] 赛博乐园 / cyberland
c++·算法·图论
洛阳纸贵7 小时前
MATLAB-matlab基础知识
学习·算法·matlab
落羽的落羽8 小时前
【AI】快速理解AI应用的相关名词概念
linux·c++·人工智能·python·计算机网络·算法
Nil2088 小时前
leetcode 17电话号码的字母组合
算法·leetcode·职场和发展
203号居民9 小时前
LeetCode hot 100 — 25. K 个一组翻转链表
算法·leetcode·链表
ocean210310 小时前
2025-2026年AI算法与模型研发面试高频知识点洞察
人工智能·算法·面试
Nil20810 小时前
leetcode 78子集
数据结构·算法·leetcode