整数删除
思路:数组模拟链表+优先队列
注意:求和需要开 longlong 不然会超时
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll,ll>;
const int N = 5e5+10;
//双向数组维护节点的顺序
ll e[N];
int l[N],r[N];
int n,k,i;
//优先队列确定元素最小值和位置
void deletex(int i){
e[l[i]]+=e[i];
e[r[i]]+=e[i];
r[l[i]]=r[i];
l[r[i]]=l[i];
}
void solve( ){
cin>>n>>k;
priority_queue<PII,vector<PII>,greater<PII>> pq;
r[0]=1;
l[n+1]=n;
for(int i=1;i<=n;i++){
ll res;cin>>res;
pq.push({res,i});
e[i] = res;
l[i]=i-1;
r[i]=i+1;
}
while(k--){
auto [res,i]= pq.top();
pq.pop();
if(e[i]!=res){
pq.push({e[i],i});
k++;
continue;
}
deletex(i);
}
for(int i=r[0];i<=n;i=r[i]){
cout<<e[i]<<" ";
}
}
int main( ){
cin.tie(nullptr)->sync_with_stdio(false);
int T=1;
while(T--){
solve();
}
return 0;
}
没取消输入输出同步:
取消输入输出同步: