1504: ZZ的橱柜(长沙理工oj)
http://www.acmore.net/problem.php?id=1504
利用优先队列,转化成O(m)的复杂度。
#include <iostream> #include <stdio.h> #include <string> #include <string.h> #include <algorithm> #include <math.h> #include <fstream> #include <vector> #include <map> #include <queue> #include <stack> using namespace std ; const int maxn = 400005; int n,m; int a[maxn],b[maxn]; struct node{ int i,j; int sum; void set(int ii,int jj,int s){ i = ii; j = jj; sum = s; } friend bool operator<(const node &a,const node &b){ return a.sum>b.sum; } }; priority_queue<node>que; int sum[maxn]; int main(){ //freopen("test.txt","r",stdin); //freopen("out.txt","w",stdout); while(~scanf("%d%d",&n,&m)){ memset(sum,0,sizeof(sum)); while(!que.empty()) que.pop(); for(int i = 0;i < n;i++) scanf("%d",a+i); for(int i = 0;i < n;i++) scanf("%d",b+i); sort(a,a+n); sort(b,b+n); node temp; for(int i = 0;i < n;i++){ temp.set(i,0,a[i]+b[0]); que.push(temp); } node x,y; /*while(!que.empty()){ printf("%d ",que.top().sum); que.pop(); }*/ for(int i = 0;i < m;i++){ x = que.top(); //printf("%d %d %d\n",x.i,x.j,x.sum); que.pop(); sum[i] = x.sum; y.set(x.i,x.j+1,a[x.i]+b[x.j+1]); que.push(y); } for(int i = 0;i < m;i++) printf("%d\n",sum[i]); } return 0 ; }