ZZ的橱柜

来源:互联网 发布:淘宝客服聊天字体颜色 编辑:程序博客网 时间:2024/05/17 08:00

Problem Description

ZZ不仅喜欢买衣服,还是个吃货,天哪!现在的妹纸是怎么了?╮(╯_╰)╭,ZZ有两个大型橱柜A和B,里面放满了零食,每个橱柜里面有N个方格,每个方格里面放了不同重量的美味,现在ZZ要从A、B里面各取一个方格的美味,可是ZZ不想变得太胖,于是他会尽量的选择重量少的美味,当然不是所有的组合都是ZZ喜欢吃的美味,所以她想了一个特别的方法,他会选出组合重量最小的前M种中选出一种,可是怎么才能算出前M个组合的重量的呢?这个真是个难题,作为ACMer的ZZ竟然不能解决,你能帮帮她么?

Input

有多组测试数据;
第一行给你一个N(N<=400000)和一个M(M<=N),N表示每个壁橱有N个方格,M表示要选出M中方案;
第二行和第三行分别有N个整数,表示每个方格的美味重量(结果保证在int以内)

Output

输出前M种方案的重量,每种方案占一行。

Sample Input

2 21 23 4

Sample Output

45

Author

HYNU

# include <cstdio># include <queue># include <vector># include <algorithm>using namespace std;# define Max  400001struct node{int x,y,v;friend bool operator < (const node &a,const node &b){return a.v>b.v;}};priority_queue <node , vector < node > >  v;int a[Max],b[Max];int n,m;int main( ){freopen("a.txt","r",stdin);node nd;while(scanf("%d%d",&n,&m)!=EOF){while(!v.empty())  v.pop();int i;for(i=0;i<n;i++)   scanf("%d",&a[i]);for(i=0;i<n;i++)   scanf("%d",&b[i]);sort(a,a+n);sort(b,b+n);for(i=0;i<n;i++){nd.x=0;nd.y=i;nd.v=a[0]+b[i];v.push(nd);}for(i=0;i<m;i++){nd=v.top();v.pop();printf("%d\n",nd.v);nd.x++;nd.v=a[nd.x]+b[nd.y];v.push(nd);}}return 0;}


0 0