较简单的排队打水,合并果子。。。
合并序列
这是一个堆的问题,就用stl里的优先队列完美搞掉
有两个序列
先sort一下
在算出最小的a[1]+b[1]…..a[1]+b[n]为一组
删掉+如a[i][j+1]
放进去
找最小的k个1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40#include<cstdio>
#include<map>
#include<string>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
struct node{
int x,y,value;
bool operator<(const node &v) const{
return v.value<value;
}
};
priority_queue<node>que;
int a[199099],b[199909],n;
int main(){
scanf("%d",&n);
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+n+1);
sort(b+1,b+n+1);
for(int i=1;i<=n;i++) {
node p;
p.x=i;
p.y=1;
p.value=a[i]+b[1];
que.push(p);
}
while(n--)
{
node p=que.top();
printf("%d ",p.value);que.pop();
node q;
q.y=p.y+1;
q.x=p.x;
q.value=a[q.x]+b[q.y];
que.push(q);
}
}
中位数
维护一个大根堆和一个小根堆
一直使小根堆的数量+1=大根堆的数量
输出大根堆的头
1 | #include<cstdio> |