堆的应用

本文深入探讨了排序算法和数据结构的应用,包括常见的排序算法(如快速排序、归并排序)及其复杂度分析,以及数据结构(如二叉树、链表、哈希表)在实际问题解决中的应用实例。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <cmath>
#define N 400005
#define INF 0x6f6f6f6f
#define debug(a)
cout<<#a<<' '<<a<<endl;
typedef long long LL;
using namespace std;
struct node{int a,b,f;
node (int x,int y,int z)
{
a=x,b=y,f=z;
}
node()
{}friend bool operator<(node x,node y)
{
if(x.a+x.b==y.a+y.b)
return x.a>y.a;
else 
return x.a+x.b>y.a+y.b;
}
};
int a[N],b[N];
int n,m;
int ans[N];
int main()
{
#ifndef ONLINE_JUDGEfreopen("in.txt","r",stdin);
#endif
while(scanf("%d %d",&n,&m)!=EOF)
{
priority_queue<node> q;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
scanf("%d",&b[i]);
sort(a+1,a+1+n);
sort(b+1,b+1+n);
int l=0;
for(int i=1;i<=n;i++)
{
q.push(node(a[i],b[1],1));
}
node tmp;
while(l<m)
{
tmp=q.top();
q.pop();
ans[l++]=tmp.a+tmp.b;
tmp.b=b[++tmp.f];
q.push(tmp);
}
for(int i=0;i<l;i++)
printf("%d\n",ans[i]);
}
return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值