为什么我的代码有误
查看原帖
为什么我的代码有误
162196
伟大的王夫子楼主2021/2/15 22:07

请同学们看看

#include <bits/stdc++.h>
using namespace std; 
int n, k, l[500010], r[500010];
bool v[500010];
typedef long long ll;
ll a[500010], ans;
priority_queue<pair<ll, int> > q;
int main() {
	//freopen("data.in", "r", stdin);
	cin >> n >> k;
	for (register int i = 1; i <= n; ++i) 
		scanf("%lld", a + i), l[i] = i - 1, r[i] = i + 1, q.push(make_pair(a[i], i));
	r[0] = 1;
	l[n + 1] = n;
	while (k--) {
		while (v[q.top().second]) q.pop();
		int y = q.top().first, x = q.top().second;
		q.pop();
		if (y < 0) break;
		ans += y;
		a[x] = a[l[x]] + a[r[x]] - a[x];
		v[l[x]] = 1, v[r[x]] = 1;
		l[x] = l[l[x]], r[l[x]] = x;
		r[x] = r[r[x]], l[r[x]] = x;
		q.push(make_pair(a[x], x));
	}
	cout << ans;
}

2021/2/15 22:07
加载中...