优先队列

较简单的排队打水,合并果子。。。

合并序列
这是一个堆的问题,就用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
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
41
42
43
#include<cstdio>
#include<map>
#include<string>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;

priority_queue<int>que;
priority_queue<int>que2;
int a[190999],b[190999],n;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{

scanf("%d",&a[i]);

if(i==1)
{
que2.push(-a[i]);
printf("%d\n",a[i]);
continue;
}
if(a[i]>que2.top()) que2.push(-a[i]);
else que.push(a[i]);
if(que.size()<que2.size())
{
que.push(-que2.top());
que2.pop();
}
if(que.size()>que2.size())
{
que2.push(-que.top());
que.pop();
}
if(i%2==1)
printf("%d\n",-que2.top());
}


}
最近的文章

并查集专题

洛谷P3144 [USACO16OPEN]关闭农场关闭农场离线的反着的并查集看看在不在一个集合内1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950#include&lt …

于  并查集 继续阅读
更早的文章

关于二维偏序的题

简单的逆序对可以用树状数组搞就是插入时,找前面比他大的和插入时弄一个桶,记下有多少个数然后对后面的进行区间求和,这个可以用树状数组搞最后求一个总和1234567891011121314151617181920212223242526272829303132#include&lt;iostream&g …

于  二维偏序 继续阅读