求助分析一下时间复杂度
查看原帖
求助分析一下时间复杂度
807774
_Wind_Leaves_ShaDow_楼主2024/10/23 13:59

rt,这题模拟赛考的,模拟赛数据 A 了,测了这题也 A 了。但是感觉时间复杂度是假的,有没有人给个 hack 或者证下复杂度的正确性/kk。

#include <bits/stdc++.h>
#define lint __int128
#define int long long
#define Il inline
#define Rg register
#define Ri Rg int
#define fi first
#define se second
#define vec vector
#define pb push_back
#define IT ::iterator
#define pque priority_queue

using namespace std;
//typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
const int N=1e5;
const db eps=1e-9,pi=acos(-1.0);

int n,m,tmp=0,ans=0;
pii a[N+5];
pque<int>pq;
pque<int,vec<int>,greater<int>>et;

signed main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>n>>m;for(Ri i=1;i<=n;i++)cin>>a[i].fi>>a[i].se;
	sort(a+1,a+n+1);
	for(Ri i=1;i<=n;i++){
		int d=a[i].fi-a[i-1].fi;
		if(m>=d)m-=d;
		else{
			while(m<d&&!pq.empty()){
				m+=pq.top();tmp--;
				et.push(pq.top()),pq.pop();
			}
			if(m<d)break;m-=d;
		}
		et.push(a[i].se);
		while(!et.empty()&&m-et.top()>=0){
			m-=et.top();tmp++;
			pq.push(et.top());et.pop();
		}
		while(!et.empty()&&!pq.empty()&&pq.top()>et.top()){
			int fp=pq.top(),fe=et.top();pq.pop(),et.pop();
			pq.push(fe),et.push(fp),m+=fp-fe;
		}
		ans=max(ans,tmp);
	}
	cout<<ans;
	return 0;
}
2024/10/23 13:59
加载中...