社区讨论

蜜汁60分求助

P1484种树参与者 5已保存回复 4

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
4 条
当前快照
1 份
快照标识符
@mi85zga3
此快照首次捕获于
2025/11/21 09:11
4 个月前
此快照最后确认于
2025/11/21 09:11
4 个月前
查看原帖
用的贪心加优先队列
原来没开longlong是50pts
现在开了成60惹qwq
求dalao帮帮这只小可耐吧呜呜
CPP
#include<bits/stdc++.h>
#define maxn 500010
#define int long long
using namespace std;

inline int read(){
	int x=0,t=1; char ch=getchar();
	while(ch<'0'&&ch>'9') ch=getchar();
	if(ch=='-') t=-1,ch=getchar();
	while(ch<='9'&&ch>='0') x=x*10+ch-48,ch=getchar();
	return x*t;
}

struct node{
	int val,pos;
	inline bool operator <(const node &x) const{
		return x.val>val;
	}
};
int n,k;
struct A{
	int val,l,r;
}a[maxn];
priority_queue<node> q;
int ans;
bool vis[maxn];

inline void change(int x){
	a[x].l=a[a[x].l].l;
	a[x].r=a[a[x].r].r;
	a[a[x].l].r=x;
	a[a[x].r].l=x;
}

signed main(){
	n=read(); k=read();
	for(register int i=1;i<=n;++i){
		a[i].val=read();
		a[i].l=i-1;
		a[i].r=i+1;
		q.push((node){a[i].val,i});
	}
	for(register int i=1;i<=k;++i){
		while(vis[q.top().pos]) q.pop();
		node now=q.top(); q.pop();
		if(now.val<=0) break;
		ans+=now.val;
		vis[a[now.pos].l]=vis[a[now.pos].r]=1;
		a[now.pos].val=a[a[now.pos].l].val+a[a[now.pos].r].val-a[now.pos].val;
		q.push((node){a[now.pos].val,now.pos});
		change(now.pos);
	}
	printf("%lld\n",ans);
	return 0;
}

回复

4 条回复,欢迎继续交流。

正在加载回复...