返回题解分享
讨论 / 题解分享/ 帖子详情

整数删除(编程题) - 题解

#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int>pii;
const int N=5e5+10;
int a[N],l[N],r[N];
int st[N];
void solve(){
	int n,k;
	cin>>n>>k;
	priority_queue<pii,vector<pii>,greater<pii>>q;
	for(int i=0;i<n;i++){
		cin>>a[i];
		q.push({a[i],i});
		
		st[i]=a[i];
		
		l[i]=i-1;
		r[i]=i+1;
		
		if(r[i]==n) r[i]=-1;
	}

	while(k){
		pii t=q.top();
		
		q.pop();
		
		if(t.first!=st[t.second]){
			q.push({st[t.second],t.second});
			continue;
		}
		k--;
		int pos=t.second;
		
		if(l[pos]>=0) st[l[pos]]+=t.first;
		
		if(r[pos]>=0)	st[r[pos]]+=t.first;
		
		if(l[pos]>=0) r[l[pos]]=r[pos];
		
		if(r[pos]>=0) l[r[pos]]=l[pos];
		
		st[pos]=-1;
		
		
	}
	for(int i=0;i<n;i++){
		if(st[i]!=-1){
			cout<<st[i]<<" ";
		}
	}
	cout<<endl;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t = 1;
	// cin >> t;
	while(t--)
	solve();
	return 0;
}
0 回复 0 转发 0 喜欢 6 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!