P3572 [POI2014] PTA-Little Bird

[POI2014] PTA-Little Bird - 洛谷

核心思路

注意力惊人。

注意到,只有两种决策 选高过自己的树中代价最小的

或者 选低于自己的树种代价的最小的+1。

取最小值

显然 不等式: 恒成立。

由此,维护一个优先队列即可。

AC 代码

cpp 复制代码
#include <bits/stdc++.h>
#define N 1000008
using namespace std;
int d[N],f[N];
int n;
struct node{
	int d,j,f;//高度 位置 代价
	bool operator < (const node &a)const {
		return (a.f == f?d < a.d:f > a.f);
	} 
}; 
int check(int mid){
	priority_queue<node> q;
	memset(f,0,sizeof(f));
	f[1] = 0;
	q.push(node{d[1],1,f[1]});
	for(int i = 2;i <= n;i++){
		node val;
		while(!q.empty()){
			val = q.top();
			if(q.top().j < i-mid)q.pop();
			else break;
		}
		f[i] = val.f + (val.d <= d[i]?1:0);
		q.push(node{d[i],i,f[i]});
	}
	return f[n];
}
int main(){
	ios::sync_with_stdio(0);
	cin>>n;
	for(int i = 1;i <= n;i++){
		cin>>d[i];
	}
	int q;
	cin>>q;
	while(q--){
		int x;
		cin>>x;
		cout<<check(x)<<endl;
	}
	return 0;
} 
相关推荐
mit6.8241 天前
dijk|tire+floyd+dp %
算法
独自破碎E1 天前
【总和拆分 + 双变量遍历】LCR_012_寻找数组的中心下标
数据结构·算法
WBluuue1 天前
Codeforces 1076 Div3(ABCDEFG)
c++·算法
u0109272711 天前
模板编译期排序算法
开发语言·c++·算法
GIS瞧葩菜1 天前
Cesium 轴拖拽 + 旋转圈拖拽 核心数学知识
人工智能·算法·机器学习
m0_686041611 天前
C++中的适配器模式变体
开发语言·c++·算法
txzrxz1 天前
结构体排序,双指针,单调栈
数据结构·算法·双指针算法·单调栈·结构体排序
AndrewHZ1 天前
【AI黑话日日新】什么是AI智能体?
人工智能·算法·语言模型·大模型·llm·ai智能体
恒者走天下1 天前
cpp c++辅导星球价格调整
c++
wWYy.1 天前
算法:二叉树最大路径和
数据结构·算法