1 条题解

  • -1
    @ 2024-1-5 21:07:09
    #include<bits/stdc++.h>
    using namespace std;
    const int N = 1e5 + 10, inf = 0x3f3f3f3f3f;
    struct P{
    	int val, l, r;
    };
    struct T{
    	int id,sub;
    	bool operator < (const T &t) const {
    		return sub > t.sub;
    	}
    };
    P p[N];
    int a[N];
    bool vis[N];
    long long ans;
    int main(){
        ios::sync_with_stdio(false);
        cin.tie(0),cout.tie(0);
    	int n, k;
    	cin >> n >> k;
    	priority_queue<T> heap;
    	cin >> a[0];
    	for (int i = 1; i < n; i ++){
    		cin >> a[i];
    		p[i].val = a[i] - a[i-1];
    		p[i].l = i-1, p[i].r = i + 1;
    		heap.push({i,p[i].val});
    	}
    	p[0].val = p[n].val = inf;
    	for (int i = 1; i <= k; i ++){
    		while(vis[heap.top().id]) heap.pop();
    		T t = heap.top();
    		heap.pop();
    		ans += t.sub;
    		int tl = p[t.id].l, tr = p[t.id].r;
    		vis[tl] = vis[tr] = true;
    		p[t.id].val = p[tl].val + p[tr].val - p[t.id].val;
    		heap.push({t.id,p[t.id].val});
    		p[t.id].l = p[tl].l, p[t.id].r = p[tr].r;
    		p[p[t.id].l].r = t.id, p[p[t.id].r].l = t.id;
    	}
    	cout << ans;
    	return 0;
    }
    
    • 1

    信息

    ID
    1122
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    (无)
    递交数
    4
    已通过
    4
    上传者