WA 50pts MnZn求助
查看原帖
WA 50pts MnZn求助
302356
PLDIS楼主2022/2/2 22:18

RT,调疯了

#include <bits/stdc++.h>
#define int long long
using namespace std;
int tot = 0;
struct node{
    int val, id;
    node *prev, *next;
};
node *head, *tail;
void init(){
    head = new node();
    head->id = ++tot;
    head->val = 0;
    tail = new node();
    tail->id = ++tot;
    tail->val = 0;
    head->next = tail;
    tail->prev = head;
}
void add(node *k, int n){
    node *t = new node();
    t->val = n;
    t->id = ++tot;
    t->next = k->next;
    t->prev = k;
    k->next->prev = t;
    k->next = t;
}
void del(node *k){
    k->prev->next = k->next;
    k->next->prev = k->prev;
    delete k;
}
void recycle(){
    while(head != tail){
        head = head->next;
        delete head->prev;
    }
}
bool operator>(pair<int, node*> a, pair<int, node*> b){
    return a.first > b.first;
}
bool operator<(pair<int, node*> a, pair<int, node*> b){
    return a.first < b.first;
}
priority_queue<pair<int, pair<int, node* > >, vector<pair<int, pair<int, node*> > >, greater<pair<int, pair<int, node*> > > > pq;
bool vis[1000001];
int a[1000001];
signed main(){
    int n, k;
    cin >> n >> k;
    for(int i = 0;i < n;i++){
        cin >> a[i];
    }
    init();
    for(int i = 1;i < n;i++){
        add(tail->prev, a[i] - a[i - 1]);
        pq.push(make_pair(a[i] - a[i - 1], make_pair(tail->prev->id, tail->prev)));
    }
    int ans = 0;
    for(int i = 0;i < k;i++){
        while(pq.size() && vis[pq.top().second.first]){
            pq.pop();
        }
        pair<int, pair<int, node* > > t = pq.top();
        ans += t.first;
        //cerr << t.first << " ";
        vis[t.second.second->next->id] = true;
        vis[t.second.second->prev->id] = true;
        vis[t.second.second->id] = true;
        node* pp;
        if(t.second.second->prev == head){
            pp = head;
        }
        else{
            pp = t.second.second->prev->prev;
        }
        add(pp, t.second.second->prev->val + t.second.second->next->val - t.second.second->val);
        pq.push(make_pair(t.second.second->prev->val + t.second.second->next->val - t.second.second->val, make_pair(t.second.second->prev->prev->id, t.second.second->prev->prev)));
        if(t.second.second->prev != head){
            del(t.second.second->prev);
        }
        if(t.second.second->next != tail){
            del(t.second.second->next);
        }
        del(t.second.second);
    }
    cout << ans << endl;
    recycle();
    return 0;
}
2022/2/2 22:18
加载中...