蓝桥杯每日一题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;
}
相关推荐
NEFU_Jyj9 小时前
牛客周赛R168
算法
Navigator_Z16 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
D e re k ZH17 小时前
C++ 标准库算法全景指南
数据结构·c++·算法
冻柠檬飞冰走茶18 小时前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划
雪落漂泊18 小时前
C++算法 二分查找篇(上)
开发语言·c++·算法
LabVIEW开发20 小时前
LabVIEW 连接板:28 个端子怎么排,优先级怎么定
java·算法·labview
严肃小白兔21 小时前
MATLAB 数值计算中的精度与误差问题:浮点运算、误差传播与数值稳定性
人工智能·算法·matlab
青少儿编程课堂1 天前
蓝桥杯青少组图形化省考报名倒计时·全国青少年信息素养大赛·电子学会图形化考级:校园黑白棋人机对战解析
c++·python·算法·bfs·信息学竞赛
jktantan1 天前
注解式脱敏:用 Jackson 的 ContextualSerializer 做字段级策略(一)
spring boot·后端·算法·spring cloud·系统架构