Untitled

 avatar
unknown
c_cpp
5 months ago
2.2 kB
7
Indexable
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <numeric>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;
#define ll long long
#define ull unsigned long long
#define f first
#define s second
#define PF push_front
#define PB push_back
#define MP make_pair

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

#define max(a, b) ((a > b)? a : b)
#define min(a, b) ((a < b)? a : b)
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)

const int N = 1e5 + 5;
const int M = 1e9 + 7; 
const int inf = 0x3f3f3f3f;
const ll int INF = 1e18;

void trans(int n, int d, vector<int> &v){
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> low;
    priority_queue<pair<int, int>, vector<pair<int, int>>> high;

    vector<int> ans;
    vector<bool> used(n, false);
    for(int i = n-1; i >= 0; i--){
        while(!high.empty() && high.top().f > v[i] + d){
            if(!used[high.top().s]) ans.PB(high.top().f);
            used[high.top().s] = true;
            high.pop();
        }

        vector<int> temp;
        while(!low.empty() && low.top().f < v[i] - d){
            if(!used[low.top().s]) temp.PB(low.top().f);
            used[low.top().s] = true;
            low.pop();
        }
        for(int j = (int)temp.size() - 1; j >= 0; j--) ans.PB(temp[j]);


        low.push(MP(v[i], i));
        high.push(MP(v[i], i));
    }

    while(!high.empty()){
        if(!used[high.top().s]) ans.PB(high.top().f);
        high.pop();
    }

    reverse(all(ans));
    v = ans;
}

int main(){
    
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    
    // ifstream cin();
    // ofstream cout();
    
    int n, d;
    cin >> n >> d;

    vector<int> v(n);
    for(int i = 0; i < n; i++){
        cin >> v[i];
    }

    trans(n, d, v);
    trans(n, d, v);
    trans(n, d, v);

    for(int i = 0; i < n; i++){
        cout << v[i] << ' ';
    }
    
}
Editor is loading...
Leave a Comment