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

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

#include <bits/stdc++.h>
#define int long long
#define int long long
#define x first
#define y second
using namespace std;

const int N = 5e5 + 10;
int n, k;
int l[N], r[N];
int a[N];

priority_queue<pair<int,int>,vector<pair<int,int>>, greater<pair<int, int>>> aa;

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> k;
    r[0] = 1;
    for(int i = 1; i <= n; i ++ )
    {
        cin >> a[i];
        aa.push({a[i], i});
        l[i] = i - 1, r[i] = i + 1;
    }

    while(k -- )
    {
        auto t = aa.top();
        aa.pop();

        if(t.x != a[t.y]) aa.push({a[t.y], t.y}), k++;
        else{
            r[l[t.y]] = r[t.y], l[r[t.y]] = l[t.y];
            a[l[t.y]] += a[t.y], a[r[t.y]] += a[t.y];
        }
    }

    int t = r[0];
    while(t != n + 1)
    {
        cout << a[t] << " ";
        t = r[t];
    }
}
0 回复 0 转发 0 喜欢 1 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!