跳到正文

题解

AT_abc379_d 题解

ikunTLE约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年11月9日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 2 分钟
获赞
15

版本

  1. v1当前

题目传送门

思路

首先,不难考虑到 O(Q2)\mathcal{O}(Q^2) 的暴力解决方案:每一次操作 2 将所有的植物都增加 TT;操作 3 挨个统计高度超过 HH 的植物数量。

然后,想到操作 2 中可以增加一个变量控制增长的高度 hh,将操作 2 的时间复杂度降至 O(1)\mathcal{O}(1)。此后要使操作 1 的初始高度为 00,就要将其赋值为 −h-h。

最后,使用优先队列降低复杂度。在操作 3 中,每次都删到比 HH 低为止。由于所有的植物数量不会超过 QQ,故总时间复杂度为 O(Qlog⁡Q)\mathcal{O}(Q\log Q),可以通过此题。

注意事项

  • 不开 long long 见祖宗。

AC CODE

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
int read(){int x=0;char f=1,ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();return x*f;}
priority_queue<int/*,vector<int>,greater<int>*/>pq;
signed main(){
	int q=read(),h=0;
	while(q--){
		int op=read();
		if(op==1)
			pq.push(-h);
		else if(op==2)
			h+=read();
		else{
			int x=read(),ans=0;
			while(!pq.empty()){
				int temp=pq.top();
				if(temp+h>=x)
					++ans,pq.pop();
				else break;
			}
			printf("%lld\n",ans);
		}
	}
	return 0;
}
原文
存档评论3 条抓取于