#小红的口罩--优先队列

题目:

链接:https://ac.nowcoder.com/acm/contest/65955/B链接:https://ac.nowcoder.com/acm/contest/65955/B

来源:牛客网

思路:

思路一:

结构:数组

函数:sort

构建一个数组储存不舒适度,按照从小到大的顺序排列,并且随着佩戴次数增多对数组元素进行翻倍处理。

代码:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	int n, k, day = 0, sum = 0;
	int i, j;
	cin >> n >> k;
	vector<int>a(n);
	for (i = 0; i < n; i++)cin >> a[i];
	sort(a.begin(), a.end());
	sum = a[0];
	while (sum < k) {
		day++;
		cout << a[0] << endl;
		a[0] *= 2;
		sort(a.begin(), a.end());
		sum += a[0];
	}
	cout << day;
}

思路二:

结构:优先列表

函数:priority_queue

构建一个优先队列储存不舒适度,通过greater实现按照从小到大的顺序排列,并且随着佩戴次数增多对队列元素进行翻倍处理。

代码:

#include<iostream>
#include<queue>
using namespace std;
int main()
{
	int n, k;
	cin >> n >> k;
	priority_queue<int, vector<int>, greater<int>>q;
	for (int i = 0; i < n; i++)
	{
		int t;
		cin >> t;
		q.push(t);
	}
	int sum = q.top();
	int day = 0;
	while (sum < k) {
		day++;
		int temp = q.top();
		q.pop();
		q.push(2 * temp);
		sum += q.top();
	}
	cout << day;
}

#小红的口罩--优先队列#
全部评论

相关推荐

能干的三文鱼刷了100道题:公司可能有弄嵌入式需要会画pcb的需求,而且pcb能快速直观看出一个人某方面的实力。看看是否有面试资格。问你问题也能ai出来,pcb这东西能作假概率不高
点赞 评论 收藏
分享
在开会的单身狗很有一套:学院本被想着这么快有面试,而且简历废话太多了 那些在校经历什么荣誉什么的企业不关心
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务