上次讲了堆,别人都说极其简单,我却没学过,今天又听dalao们讲图论,最短路又用堆优化,问懂了没,底下全说懂了,我???,感觉全世界都会了堆,就我不会,于是我决定补一补;

——————来自百度百科

所以,堆其实就是一棵树;

大根堆:根节点比子节点权大;

小根堆:根节点比子节点权小;

了解到了这里,我觉得可以开始做题了;

T1合并果子

题面自己去洛谷看(我懒)

就是一个小根堆,每次取最小的两堆果子合并,排序会tle,所以用堆做,每次把合并后的再加入堆中就行了;

为了练习,先来一个手写堆;

详细看代码:

#include<iostream>
#include<cstdio>
#include<cctype>
#include<cmath>
using namespace std;
inline int read()
{
int x=,f=;char c=getchar();
while(!isdigit(c)){if(c=='-')f=-;c=getchar();}
while(isdigit(c)){x=(x<<)+(x<<)+(c^);c=getchar();}
return x*f;
}
int dui[],n,m,ans;
inline void down(int x)
{
if(x*<=n)
{
int t=x;
if(dui[x]>dui[x*])t=x*;
if(dui[x]>dui[x*+]&&x*+<=n&&dui[x*+]<dui[x*])t=x*+;
if(x==t)return;
swap(dui[x],dui[t]);
down(t);
}
}
inline void up(int x)
{
if(x>)
{
if(dui[x]<dui[x/]){
swap(dui[x],dui[x/]);
up(x/);
}
}
}
inline int top(){return dui[];}
inline void delet()
{
dui[]=dui[n];
dui[n--]=;
down();
}
inline void add(int x)
{
dui[++n]=x;
up(n);
}
int main()
{
m=read();
for(int i=;i<=m;i++)
{
int a=read();
add(a);
}
for(int i=;i<m;i++)
{
int x=top();
delet();
int y=top();
delet();
ans+=(x+y);
add(x+y);
}
cout<<ans<<endl;
return ;
}
 #include<iostream>
#include<cctype>//为了用isdigit
#include<cstdio>
using namespace std;
inline int read()//快读(isdigit用来判读入的是否是数字,据说比c>'0'这样的快) ;
{
int x=,f=;char c=getchar();
while(!isdigit(c)){if(c=='-')f=-;c=getchar();}
while(isdigit(c)){x=(x<<)+(x<<)+(c^);c=getchar();}
return x*f;
}
int n,dui[],size;//dui用来存堆,size记录元素个数;
inline void swap(int &a,int &b){int t=a;a=b;b=t;return;}//交换值,&直接取地址交换,据说手写更快;
inline void up(int i)//把堆里元素向上移动
{
while(i>)//直到查询到堆顶
if(dui[i]<dui[i/])//如果i的元素比它父节点小,则交换两个元素;
{
swap(dui[i],dui[i/]);
i/=;//继续向上查询
}
else return;
}
inline void init(int x){dui[++size]=x;up(size);}//插入新的元素,size+1,然后看新元素是否要往上移动;
void down(int i)//向下移动大的元素
{
int s=i*;//i的左儿子
while(s<=size)
{
if(dui[i*+]<dui[i*])s++;//从i的左儿子和右儿子中选择较小的交换
if(dui[i]>dui[s])//如果i的权值比i的儿子的权值大,将i向下移动
{
swap(dui[i],dui[s]);
i=s;s=i*;//注意!!!一定要i=s,我忘了会+1卡了好久,左子树和右子树一定要分清楚!!!
}
else return;
}
}
void shanchu(){dui[]=dui[size--];down();}//合并后删除堆顶元素 ,并将元素向下传递;
int top(){return dui[];}//取出堆顶元素
int main()
{
n=read();
int a;
for(int i=;i<=n;i++)
{
a=read();init(a);
}
long long answer=;
while(size>=)
{
int a1=top();
shanchu();
int a2=top();
shanchu();//取出堆中最小的的两堆果子
answer+=(a1+a2);
init(a1+a2);//将合并后的一堆果子加入堆中
}
printf("%lld\n",answer);
return ;
}

偷懒做法,用STL

 #include<iostream>
#include<set>
using namespace std;
multiset<int>dui;//multiset会自动帮你排序,从小到大,设它是一个堆
int n;
int main()
{
int a;
cin>>n;
for(int i=;i<=n;i++)
{
cin>>a;
dui.insert(a);//把元素放进堆里
}
int ans=;
for(int i=;i<=n-;i++)
{
int a1=*dui.begin();//dui.begin()返回的是地址,*取出地址里的值;
dui.erase(dui.begin());//删除堆顶,用地址传递
int a2=*dui.begin();
dui.erase(dui.begin());
ans+=(a1+a2);
dui.insert(a1+a2);//将合并后的加入堆
}
cout<<ans<<endl;
return ;
}

未完待续,如果我忘了可以催更(虽然没人看我博客)

堆学习笔记(未完待续)(洛谷p1090合并果子)的更多相关文章

  1. Go web编程学习笔记——未完待续

    1. 1).GOPATH设置 先设置自己的GOPATH,可以在本机中运行$PATH进行查看: userdeMacBook-Pro:~ user$ $GOPATH -bash: /Users/user/ ...

  2. linux学习笔记---未完待续,缓慢更新

    做为linux菜鸟,由于work的需要,慢慢的开始接触学习linux. <鸟哥的linux私房菜>学习笔记. 一.基础命令操作 1.显示日期的命令 date 执行date命令后,显示结果为 ...

  3. 【洛谷P1090 合并果子】

    题目描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和.可 ...

  4. 洛谷 P1090合并果子【贪心】【优先队列】

    题目描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和.可 ...

  5. [NOIP2004] 提高组 洛谷P1090 合并果子

    题目描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和.可 ...

  6. 洛谷P1090 合并果子【贪心】

    在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和.可以看出,所 ...

  7. 洛谷P1090——合并果子(贪心)

    https://www.luogu.org/problem/show?pid=1090 题目描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合 ...

  8. jQuery 学习笔记(未完待续)

    一.jQuery概述    宗旨: Write Less, Do More.    基础知识:        1.符号$代替document.getElementById()函数        2.使 ...

  9. oracle-绑定变量学习笔记(未完待续)

    --定义变量SQL> var a number; --给绑定变量赋值SQL> exec :a :=123; PL/SQL procedure successfully completed. ...

随机推荐

  1. a=(1,)b=(1),c=(“1”) 分别是什么类型的数据

    (1,)– tuple; (“1”) – str; (1) – int; >>> (2,)(2,)>>> (2)2>>> ("6&quo ...

  2. c++ 珊格画椭圆

    #ifndef _TEST_H #define _TEST_H #include <iostream> #include <math.h> using namespace st ...

  3. [WEB安全]phpMyadmin后台任意文件包含漏洞分析(CVE-2018-12613)

    0x00 简介 影响版本:4.8.0--4.8.1 本次实验采用版本:4.8.1 0x01 效果展示 payload: http://your-ip:8080/index.php?target=db_ ...

  4. Arts打卡第5周

    Algorithm.主要是为了编程训练和学习. 每周至少做一个 leetcode 的算法题(先从Easy开始,然后再Medium,最后才Hard). 进行编程训练,如果不训练你看再多的算法书,你依然不 ...

  5. Filename too long Resolution

    在git bash中,运行下列命令: git config --global core.longpaths true --global是该参数的使用范围,如果只对本版本库设置该参数,只要在上述命令中去 ...

  6. 续--Flask, Django - 区别

    1. 目录结构         参考:https://blog.csdn.net/yang9520/article/details/79740374 中文文档(http://docs.jinkan.o ...

  7. 【转】nodejs接收前端formData数据

    很多时候需要利用formdata数据格式进行前后端交互. 前端代码可以是如下所示: <!DOCTYPE html> <html lang="en"> < ...

  8. [C#]加密解密 MD5、AES

    /// <summary> /// MD5函数 /// </summary> /// <param name="str">原始字符串</p ...

  9. main方法的详解

    格式 * public static void main(String[] args) {} 针对格式的解释 public 被jvm调用,访问权限足够大. static 被jvm调用,不用创建对象,直 ...

  10. ifc tree

    ViewerWidget* viewerWidget = new ViewerWidget(ifcModel); viewerWidget ->setRootNode(ifcModel-> ...