优先队列太好用了手写啥呀

poj1456 经过贪心专题的洗礼以后这题根本就不叫题啊。。。按时间大到小排每次取最大就好

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std; struct node
{
int v,d;
}a[];
bool cmp(node n1,node n2){return n1.d>n2.d;}
priority_queue<int>q;
int main()
{
int n,mmax=;
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
scanf("%d%d",&a[i].v,&a[i].d), mmax=max(mmax,a[i].d);
sort(a+,a+n+,cmp); int tp=,ans=;
while(!q.empty())q.pop();
for(int i=mmax;i>=;i--)
{
while(tp<=n&&a[tp].d>=i) q.push(a[tp].v), tp++;
if(!q.empty()) ans+=q.top(), q.pop();
}
printf("%d\n",ans);
}
return ;
}

poj1456

poj2442 这题还是很有价值的。一开始想了个貌似很对的做法,就是把每一行排完序和第一个作差,差值压进小根堆里面,堆顶出来以后和之前出去的合并,然后用两个LL的变量记录状态判重,结果后来发现只要m>=3就会有循环,然后就会重复,强行map判定又会多删。最后就是两两合并,因为只有两行所以可以省掉一个n,就能过了。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std; int a[],c[][];
struct node
{
int x,y,d1,d2;bool op;
friend bool operator>(node n1,node n2)
{
return (n1.d1+n1.d2)>(n2.d1+n2.d2);
}
};priority_queue<node,vector<node>,greater<node> >q;
void ins(int x,int y,int d1,int d2,bool op)
{
node t;
t.x=x;t.y=y;t.op=op;
t.d1=d1;t.d2=d2;q.push(t);
} void merge(int n)
{
while(!q.empty())q.pop();
ins(,,c[][],c[][],false);
for(int i=;i<=n;i++)
{
node t=q.top();q.pop();
a[i]=t.d1+t.d2; if(t.x<n)ins(t.x,t.y+,c[][t.x],c[][t.y+],true);
if(t.y<n&&t.op==false)ins(t.x+,t.y,c[][t.x+],c[][t.y],false);
}
for(int i=;i<=n;i++)c[][i]=a[i];
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int m,n,sum=;
scanf("%d%d",&m,&n);
for(int j=;j<=n;j++)scanf("%d",&a[j]);
sort(a+,a+n+);sum+=a[];
for(int j=;j<=n;j++)c[][j]=a[j]-a[]; for(int i=;i<=m;i++)
{
for(int j=;j<=n;j++)scanf("%d",&a[j]);
sort(a+,a+n+);sum+=a[];
for(int j=;j<=n;j++)c[][j]=a[j]-a[];
merge(n);
} for(int i=;i<n;i++)printf("%d ",sum+c[][i]);
printf("%d\n",sum+c[][n]);
}
return ;
}

poj2442

bzoj1150: [CTSC2007]数据备份Backup ->环形版 bzoj2151: 种树

合并果子就算了

bzoj4198 这个想一想就可以发现对应一个k叉的哈夫曼树嘛。。深度较小的话就加一个变量表示合并的次数,相同情况合并合并次数较小的就好。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL; struct node
{
LL x;int id,mr;
friend bool operator>(node n1,node n2)
{
if(n1.x==n2.x)return n1.mr>n2.mr;
return n1.x>n2.x;
}
};priority_queue<node,vector<node>,greater<node> >q;
void insert(LL x,int id,int mr)
{
node t;
t.x=x;t.id=id;t.mr=mr;
q.push(t);
} struct edge
{
int x,y,next;
}a[];int len,last[];
void ins(int x,int y)
{
len++;
a[len].x=x;a[len].y=y;
a[len].next=last[x];last[x]=len;
}
int li,deper;LL sum,c[];
void dfs(int x,int dep)
{
if(x<=li)
{
sum+=((LL)dep)*c[x];
deper=max(dep,deper);
return ;
}
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
dfs(y,dep+);
}
} int main()
{
int n,k;node t,p;
scanf("%d%d",&n,&k);li=n;
for(int i=;i<=n;i++)
scanf("%lld",&c[i]), insert(c[i],i,);
while((n-)%(k-)!=) insert(,++n,); int cc=(n-)/(k-);
len=;memset(last,,sizeof(last));
for(int i=;i<=cc;i++)
{
t.x=;t.id=++n;t.mr=;
for(int j=;j<=k;j++)
{
p=q.top();q.pop();
t.x+=p.x;t.mr+=p.mr;
ins(t.id,p.id);
}
q.push(t);
} sum=;deper=;
dfs(q.top().id,);
printf("%lld %d\n",sum,deper);
return ;
}

bzoj4198

0x17 二叉堆的更多相关文章

  1. 0x17二叉堆之超市

    题目链接:https://www.acwing.com/problem/content/147/ 容易想到一个贪心策略:在最优解中,对于每个时间(天数) t,应该在保证不卖出过期商品的前提下,尽量卖出 ...

  2. AC日记——二叉堆练习3 codevs 3110

    3110 二叉堆练习3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 给定N(N≤500,000)和N个整 ...

  3. codevs 3110 二叉堆练习3

    3110 二叉堆练习3 http://codevs.cn/problem/3110/ 题目描述 Description 给定N(N≤500,000)和N个整数(较有序),将其排序后输出. 输入描述 I ...

  4. 数据结构图文解析之:二叉堆详解及C++模板实现

    0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...

  5. POJ 2010 - Moo University - Financial Aid 初探数据结构 二叉堆

    考虑到数据结构短板严重,从计算几何换换口味= = 二叉堆 简介 堆总保持每个节点小于(大于)父亲节点.这样的堆被称作大根堆(小根堆). 顾名思义,大根堆的数根是堆内的最大元素. 堆的意义在于能快速O( ...

  6. 二叉堆(一)之 图文解析 和 C语言的实现

    概要 本章介绍二叉堆,二叉堆就是通常我们所说的数据结构中"堆"中的一种.和以往一样,本文会先对二叉堆的理论知识进行简单介绍,然后给出C语言的实现.后续再分别给出C++和Java版本 ...

  7. 二叉堆(二)之 C++的实现

    概要 上一章介绍了堆和二叉堆的基本概念,并通过C语言实现了二叉堆.本章是二叉堆的C++实现. 目录1. 二叉堆的介绍2. 二叉堆的图文解析3. 二叉堆的C++实现(完整源码)4. 二叉堆的C++测试程 ...

  8. 二叉堆(三)之 Java的实现

    概要 前面分别通过C和C++实现了二叉堆,本章给出二叉堆的Java版本.还是那句话,它们的原理一样,择其一了解即可. 目录1. 二叉堆的介绍2. 二叉堆的图文解析3. 二叉堆的Java实现(完整源码) ...

  9. 二叉堆(binary heap)

    堆(heap) 亦被称为:优先队列(priority queue),是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复提取队列中第一个作业并运行,因 ...

随机推荐

  1. python-sqlite3事务

    sqlite3事务总结: 在connect()中不传入 isolation_level 事务处理: 使用connection.commit() #!/usr/bin/env python # -*- ...

  2. lhgdialog.js弹出框

    官方学习网址: http://www.lhgdialog.com/ 个人认为它的样式不太好调,除此之外它也是一款实用的弹出框,专业的用来提示文字,消息,按钮添加function().ifame: 以下 ...

  3. elasticsear+kibana+logstash 优化

    ##关于s2安装cerebro来可视化集群管理##### https://blog.csdn.net/RWSLR6/article/details/79648767 https://github.co ...

  4. Cupid's Arrow[HDU1756]

    Cupid's Arrow Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. Android WindowManager和WindowManager.LayoutParams的使用以及实现悬浮窗口的方法

    1.理清概念 我们使用过Dialog和PopupWindow,还有Toast,它们都显示在Activity之上.那么我们首先需要理解的是android中是如何去绘制这些UI的呢?这里我只讲我所理解的, ...

  6. Android 自定义控件之圆形扩散View(DiffuseView)

    实现效果 使用 属性方法 代码 源码下载 参考链接 实现效果 使用 XML中: <com.airsaid.diffuseview.widget.DiffuseView android:id=&q ...

  7. RxSwift文档搜集与备份

    http://reactivex.io The Observer pattern done right ReactiveX is a combination of the best ideas fro ...

  8. maven中tomcat7:run无法启动maven项目

    这几天在学习ssm相关整合,在使用maven时,发现了一些问题,就是明明按代码都差不多就是没法运行 这个是maven主项目的pom.xml的配置,我解决的方法是添加 <maven.compile ...

  9. 破解sublim_Text3

    1.更改hosts文件 windows系统的hosts文件在C:\Windows\System32\drivers\etc 路径下,其他系统请自行百度 在hosts文件中加入下面两行: 127.0.0 ...

  10. 佛祖保佑,永不宕机,永无 Bug

    转自:http://top.jobbole.com/17580/ 佛祖保佑,永不宕机,永无 Bug 为何服务器频遭黑客攻击?为何系统频频宕机,别人家系统却稳如泰山,坚如磐石?为何运维人员和系统管理员行 ...