上这道题部位别的,就是因为自己脑残的毛病又犯了。
其实可以显然看出这是一道贪心,但是如果只是单纯的吧a,b这两个数组排序后贪心,那还是太天真了,因为可以很显然的发现b和a的元素和是在动态更新所以需要动态维护这个结果,自然很容易的的想到优先队列。
但是,因为这个思路是在写到一半的时候才发现的,所以原来的代码还有残留,又没有更改,自信的以为没错,结果wa了一次才发现,哎
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#define maxn 100020
using namespace std;
int n;
int a[maxn*2],ans[2*maxn],tot;
struct node{
int z,l;
node():z(0),l(1){}
bool operator<(const node& x)const{
return z<x.z;
}
}b[maxn];
typedef pair<int ,node>pii;
priority_queue<pii,vector<pii>,greater<pii> >q;
bool cmp(int a,int b){return a<b;}
int main(){
scanf("%d",&n);
int x,y;
for(int i=1;i<=n;i++)scanf("%d",a+i);
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++){
scanf("%d",&x);
b[i].z=x;b[i].l=1;
y=b[i].z+a[b[i].l];
q.push(make_pair(y,b[i]));
}
int cnt=0;
while(tot<n){
ans[++tot]=q.top().first;
node x=q.top().second;
q.pop();
x.l++;
int y=a[x.l]+x.z;
q.push(make_pair(y,x));
}
for(int i=1;i<=tot;i++){
printf("%d\n",ans[i]);
}
return 0;
}
/*
10
1 3 2 4 5
6 3 4 1 7
*/