首页 > 其他 > 详细

[Cometoj#2A]比赛_枚举/堆

时间:2019-10-23 15:39:11      阅读:98      评论:0      收藏:0      [点我收藏+]

比赛

题目链接https://cometoj.com/contest/38/problem/A?problem_id=1534

数据范围:略。


题解

原题没啥意思,就是个暴力枚举。

出了个加强版,$1\le n,k\le 10^5$。

因为$k$只有$10^5$,所以我们可以以此求出来前$k$个。

直接用堆贪心以下即可。

代码

#include <bits/stdc++.h>

#define N 1000010 

using namespace std;

typedef long long ll;

char *p1, *p2, buf[100000];

#define nc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1 ++ )

int rd() {
	int x = 0;
	char c = nc();
	while (c < 48) {
		c = nc();
	}
	while (c > 47) {
		x = (((x << 2) + x) << 1) + (c ^ 48), c = nc();
	}
	return x;
}

priority_queue <pair<int, pair<int, int> > >q;

int a[N];

int main() {
	int n = rd(), k = rd();
	for (int i = 1; i <= n; i ++ ) {
		a[i] = rd();
	}
	sort(a + 1, a + n + 1);
	for (int i = 1; i < n; i ++ ) {
		q.push(make_pair(a[i] + a[i + 1], make_pair(i, i + 1)));
	}
	ll ans = 0;
	for (int i = 1; i <= k; i ++ ) {
		int x = q.top().second.first, y = q.top().second.second;
		ans += a[x] + a[y];
		q.pop();
		if (x > 1) {
			q.push(make_pair(a[x - 1] + a[y], make_pair(x - 1, y)));
		}
	}
	cout << ans << endl ; 
	return 0;
}

 

[Cometoj#2A]比赛_枚举/堆

原文:https://www.cnblogs.com/ShuraK/p/11726226.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!