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;
} 
相关推荐
无忧.芙桃4 小时前
C++语言原理与实践(二十三):哈希表的实现
c++·链表·哈希算法·散列表·哈希表
郝学胜-神的一滴6 小时前
Effective Python 条款36:优雅拼装迭代器与生成器
开发语言·数据结构·人工智能·python·numpy·scikit-learn
AvocadoZeroOneOI7 小时前
B4500 [GESP202603 三级] 凯撒密码 详细解题报告
c++·算法
cjz38999 小时前
双指针技巧:覆盖式双指针(原地覆盖)
算法
whitelbwwww9 小时前
cuBLAS--CUDA高性能线性代数计算库--第一部分
c++·线性代数·gpu算力·cuda c++
console.log('npc')11 小时前
Python 学习教程 · 第4课:数据结构
数据结构·python·学习
eggwyw12 小时前
C++设计模式之解释器模式
开发语言·c++
橘子1313 小时前
【高级数据库】关系代数基本概念
算法
电科_银尘1 天前
【大模型算法与工程 15】手写Self-Attention:Q、K、V与缩放点积
算法
我不会插花弄玉1 天前
13.多路转接epoll【由浅入深-网络】
网络·c++·epoll