CE17.【C++ Cont】练习题组17(堆专题)

目录

1.P2085 最小函数值 

题目

分析

方法1:暴力求解

方法2:二次函数的性质(推荐!)

代码

提交结果

2.P1631 序列合并

分析

方法1:建两个堆

第一版代码

提交结果

第二版代码

提交结果

第三版代码

提交结果

方法2:只建一个堆

代码

提交结果


1.P2085 最小函数值 

题目

https://www.luogu.com.cn/problem/P2085

分析

从题目的"输出将这 n 个函数所有可以生成的函数值排序后的前 m 个元素"可以看出是堆排序的TopK问题

方法1:暴力求解

求出所有函数中的m个值,然后取前m个,n个函数各有m个值,时间复杂度过高,为O(m\cdot n),不建议使用

方法2:二次函数的性质(推荐!)

以测试用例分析画表:

会发现:x==4及之后x的值,均是x^2+7x+1最小,只有它的函数值需要入队

原因:x^2+7x+1的最高次的系数为1,阶位最高比4x^2+5x+33x^2+4x+5都要小,同是二次函数,二次项前的系数决定阶位的高低,因此有:

\lim_{x \to+ \infty} {4x^2+5x+3}>\lim_{x \to+ \infty} {3x^2+4x+5}>\lim_{x \to+ \infty} {x^2+7x+1}

从图像上也可以看出:

算法:首先将x==1的所有函数值计算出来,然后入堆,然后每次拿出最小的那个函数值来打印,把对应的之后删除堆顶元素,把对应的下一个函数值f(x+1)入堆

因为每次拿出最小的那个函数值,所以建立小根堆

因为要将对应的下一个函数值入堆,所以需要知道是哪个函数算的,因此要记录函数的编号,要算f(x+1),需要知道x,因此入堆的元素是结构体,建小根堆需要运算符重载

struct node
{
	ull f_x;//f(x)函数值 
	ull num;//函数的编号 
	ull x;
	bool operator<(const node& _node) const
	{
		return f_x>_node.f_x;//建立小根堆 
	}		
};

 例如

设f(x)=4x^2+5x+3,编号为1;g(x)=3x^2+4x+5,编号为2;h(x)=x^2+7x+1,编号为3;先将{12,1,1}、{12,2,1}、{9,3,1}入堆,发现函数值9最小,因此删除堆顶元素后将{h(1+1),3,2}入堆,以此类推......

注意:删除堆顶元素只能进行m次

代码

#include <iostream>
#include <queue>
#define endl "\n"  
using namespace std;
typedef unsigned long long ull;
const int N=1e5+10;
struct node
{
	ull f_x;//f(x)函数值 
	ull num;//函数的编号 
	ull x;
	bool operator<(const node& _node) const
	{
		return f_x>_node.f_x;//建立小根堆 
	}		
};
priority_queue<node> q;
ull A[N],B[N],C[N]; 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	ull n,m;
	
	cin>>n>>m;
	for (ull i=1;i<=n;i++)
		cin>>A[i]>>B[i]>>C[i];
	for (ull j=1;j<=n;j++)
		q.push({A[j]+B[j]+C[j],j,1});//x=1的所有函数值入队
		 
	while(m--) 
	{
		node tmp=q.top();
		cout<<q.top().f_x<<" ";
		q.pop();
		q.push({A[tmp.num]*(tmp.x+1)*(tmp.x+1)+B[tmp.num]*(tmp.x+1)+C[tmp.num],tmp.num,tmp.x+1});
	} 

    return 0;
}

提交结果

2.P1631 序列合并

fhttps://www.luogu.com.cn/problem/P1631

分析

线索"从小到大表示这 N 个最小的和",显然属于TopK问题

方法1:建两个堆

由于是从小到大,应该建大根堆,将a[i]+b[j]的结果存入其中,保持大根堆内含N个元素,如果直接打印其堆顶元素的话结果是从大到小的,可以再建一个小根堆,将大根堆的堆顶元素存入小根堆,最后再打印大根堆的堆顶元素

第一版代码

#include <iostream>
#include <queue>
using namespace std;
const int N1=1e6+10;
const int N2=1e3+10;
typedef unsigned long long ull;
ull n,a[N1],b[N1],c[N2],d[N1],s;
priority_queue<ull,vector<ull>,less<ull>> tmp_heap;
priority_queue<ull,vector<ull>,greater<ull>> result_heap;
int main()
{
	cin>>n;
	for (int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for (int i=0;i<n;i++)
	{
		cin>>b[i];
	}
	for (int i=0;i<n;i++)
	{
		for (int j=0;j<n;j++)
		{
			c[s]=a[i]+b[j];
			if (tmp_heap.size()<n)		
			{
				tmp_heap.push(c[s]);	
			} 
			else if (tmp_heap.top()>c[s])
			{
				tmp_heap.pop();
				tmp_heap.push(c[s]);
			}
			s++;
		}
	}	
	while (tmp_heap.size())
	{
		result_heap.push(tmp_heap.top());
		tmp_heap.pop();		
	} 
	while(result_heap.size())
	{
		cout<<result_heap.top()<<" ";
		result_heap.pop();
	}
	return 0;
}
提交结果

超出内存的限制,其实不用创建c[N]数组,直接使用临时变量存储a[i]+b[j]的结果,节约空间

第二版代码

#include <iostream>
#include <queue>
using namespace std;
const int N1=1e6+10;
const int N2=1e3+10;
typedef unsigned long long ull;
ull n,a[N1],b[N1],d[N1],c;
priority_queue<ull,vector<ull>,less<ull>> tmp_heap;
priority_queue<ull,vector<ull>,greater<ull>> result_heap;
int main()
{
	cin>>n;
	for (int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for (int i=0;i<n;i++)
	{
		cin>>b[i];
	}
	for (int i=0;i<n;i++)
	{
		for (int j=0;j<n;j++)
		{
			c=a[i]+b[j];
			if (tmp_heap.size()<n)		
			{
				tmp_heap.push(c);	
			} 
			else if (tmp_heap.top()>c)
			{
				tmp_heap.pop();
				tmp_heap.push(c);
			}
		}
	}	
	while (tmp_heap.size())
	{
		result_heap.push(tmp_heap.top());
		tmp_heap.pop();		
	} 
	while(result_heap.size())
	{
		cout<<result_heap.top()<<" ";
		result_heap.pop();
	}
	return 0;
}
提交结果

解决方法:有超时,说明for循环的次数过多,注意到题目的"单调不降序列",应尽量减少循环次数,观察发现,设外循环为变量i,内循环为变量j,当i不变时,随着j的增大a[i]+b[j]的值也在增大或者不变,当a[i]+b[j]在增大时,必定存在某个状态有tmp_heap.top()<c,不用入堆,应该立刻退出内循环,减少循环次数 

第三版代码

#include <iostream>
#include <queue>
using namespace std;
const int N1=1e6+10;
const int N2=1e3+10;
typedef unsigned long long ull;
ull n,a[N1],b[N1],d[N1],c;
priority_queue<ull,vector<ull>,less<ull>> tmp_heap;
priority_queue<ull,vector<ull>,greater<ull>> result_heap;
int main()
{
	cin>>n;
	for (int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	for (int i=0;i<n;i++)
	{
		cin>>b[i];
	}
	for (int i=0;i<n;i++)
	{
		for (int j=0;j<n;j++)
		{
			c=a[i]+b[j];
			if (tmp_heap.size()<n)		
			{
				tmp_heap.push(c);	
			} 
			else if (tmp_heap.top()>c)
			{
				tmp_heap.pop();
				tmp_heap.push(c);
			}
			if (tmp_heap.top()<c)
				break;		
		}
	}	
	while (tmp_heap.size())
	{
		result_heap.push(tmp_heap.top());
		tmp_heap.pop();		
	} 
	while(result_heap.size())
	{
		cout<<result_heap.top()<<" ";
		result_heap.pop();
	}
	return 0;
}
提交结果

方法2:只建一个堆

只建小根堆,由题目的"单调不降序列"

先将a[i]+b[0]入堆(堆中元素正好N个),之后每次先出堆顶元素,再入剩下的元素(a[i]+b[1]、a[i]+b[2]、...、a[i]+b[n]),这样每次出堆的元素都是当前堆中最小的,执行N次出堆即可,时间复杂度为O(NlogN)

以题目的测试用例为例

2+1=3 6+1=7 6+1=7

2+4=6 6+4=10 6+4=10

2+8=10 6+8=14 6+8=14

一行一行看,会发现:

a[1] + b[1] ≤ a[1] + b[2] ≤ a[1] + b[3] ≤ ... ≤ a[1] + b[n]
a[2] + b[1] ≤ a[2] + b[2] ≤ a[2] + b[3] ≤ ... ≤ a[2] + b[n]
a[3] + b[1] ≤ a[3] + b[2] ≤ a[3] + b[3] ≤ ... ≤ a[3] + b[n]
......

先将a[i]+b[1]入堆(因为是每一行不等式中最小的那个),之后取堆顶元素后再入堆,执行N次

能只将和入堆吗?如果不用两重循环的话

答:不能,只将和入堆会找不到下一个入堆的元素,入堆的应该是结构体

代码

#include <iostream>
#include <queue>
using namespace std;
const int N1=1e6+10;
int n,a[N1],b[N1];
struct node
{
	int i;
	int j;
	int sum;
	bool operator<(const node& x)const
	{
		return sum>x.sum;	
	}	
};

priority_queue<node> heap; 
int main() 
{
	cin>>n;
	for (int i=0;i<n;i++)
		cin>>a[i];
	for (int i=0;i<n;i++)
		cin>>b[i];
	
	//前n个入堆
	for (int i=0;i<n;i++)
	{
		heap.push({i,0,a[i]+b[0]}); 
	}	
	
	//剩下的入堆
	for (int k=0;k<n;k++)
	{
		node tmp=heap.top();
		heap.pop();		
		cout<<tmp.sum<<" ";
		if (tmp.j+1<n) 
		{
			heap.push({tmp.i,tmp.j+1,a[tmp.i]+b[tmp.j+1]}); 
		}
	 } 

	 
	return 0;
}

提交结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangcoder

赠人玫瑰手有余香,感谢支持~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值