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;
} 
相关推荐
m0_6399069423 分钟前
Redis入门|五种数据结构、使用场景、持久化RDB&AOF
数据结构·数据库·redis
Sakura_j_h_1 小时前
EasyEDA PCB 快速设计Skills
c++·python·github
ZhengEnCi1 小时前
Agent 架构怎么选:从单 Agent 到多 Agent 编排的选型决策指南
算法·架构
程序喵大人2 小时前
【C++入门】内存模型与资源管理 - 09 shared_ptr 和 weak_ptr 解决共享,也带来代价
开发语言·c++·资源管理·智能指针·内存模型
秋天的怀念_David2 小时前
第二十二夜 · 数字迷雾
java·程序人生·算法
Pniubi2 小时前
力扣283移动零(双指针法)
算法·leetcode·职场和发展
hhb_6183 小时前
大模型落地三重优化实战
人工智能·算法·机器学习
云和数据.ChenGuang3 小时前
光哥说Python RAG 系统需要哪些评估指标:从检索到生成的全链路评测指南
网络·人工智能·python·算法·langchain
jimy14 小时前
C++ 中的 `T&&`:右值引用与转发引用
开发语言·c++
2601_970703834 小时前
工业除湿机租赁:把一份租金合同拆开看
大数据·数据结构·算法·数据库架构·推荐算法