【洛谷P1090】合并果子
合并果子
贪心:每次先合并最小的两堆果子
用堆实现
#include<iostream>
#include<cstdio>
using namespace std;
int heap_size,n;
int heap[]; void swap(int &a,int &b)
{
int t=a;a=b;b=t;
} void put(int d)
{
int now,next;
heap[++heap_size]=d;
now=heap_size;
while(now>)
{
next=now>>;
if(heap[now]>=heap[next]) return;
swap(heap[now],heap[next]);
now=next;
}
} int get()
{
int now,next,res;
res=heap[];
heap[]=heap[heap_size--];
now=;
while(now<<<=heap_size)
{
next=now<<;
if(next<heap_size&&heap[next+]<heap[next]) next++;
if(heap[now]<heap[next]) return res;
swap(heap[now],heap[next]);
now=next;
}
return res;
} void work()
{
int i,x,y,ans=;
cin>>n;
for(i=;i<=n;i++)
{
cin>>x;
put(x);
}
for(i=;i<n;i++)
{
x=get();
y=get();
ans+=x+y;
put(x+y);
}
cout<<ans<<endl;
} int main()
{
ios::sync_with_stdio(false);
work();
return ;
}
手写堆真恶心。。
STL是个好东西
#include<iostream>
#include<queue>
#include<cstdio>
using namespace std; priority_queue<int,vector<int>,greater<int> >h; int n,x,ans=,tmp=; int main() { scanf("%d",&n);
for(int i = ; i <= n; i++) {
scanf("%d",&x); h.push(x);
}
for(int i = ; i <= n-; i++) {
tmp = h.top( );
h.pop( );
tmp = tmp+h.top( );
h.pop( );
h.push(tmp);
ans += tmp;
}
printf("%d",ans);
return ;
}
【洛谷P1090】合并果子的更多相关文章
- 堆学习笔记(未完待续)(洛谷p1090合并果子)
上次讲了堆,别人都说极其简单,我却没学过,今天又听dalao们讲图论,最短路又用堆优化,问懂了没,底下全说懂了,我???,感觉全世界都会了堆,就我不会,于是我决定补一补: ——————来自百度百科 所 ...
- 【洛谷P1090 合并果子】
题目描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和.可 ...
- 洛谷 P1090合并果子【贪心】【优先队列】
题目描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和.可 ...
- 洛谷P1090 合并果子
合并果子 题目链接 这个只能用于结构体中 struct item { int val; friend bool operator < (item a,item b) { return a.val ...
- [NOIP2004] 提高组 洛谷P1090 合并果子
题目描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和.可 ...
- 洛谷P1090 合并果子【贪心】
在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和.可以看出,所 ...
- 洛谷P1090——合并果子(贪心)
https://www.luogu.org/problem/show?pid=1090 题目描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合 ...
- 洛谷 p1090 合并果子
https://www.luogu.org/problemnew/show/P1090 优先队列的经典题目 体现了stl的优越性 #include<bits/stdc++.h> using ...
- Java实现 洛谷 P1090 合并果子
import java.io.BufferedInputStream; import java.util.Arrays; import java.util.Scanner; public class ...
- 代码源 每日一题 分割 洛谷 P6033合并果子
题目链接:切割 - 题目 - Daimayuan Online Judge 数据加强版链接: [NOIP2004 提高组] 合并果子 加强版 - 洛谷 题目描述 有一个长度为 ∑ai 的木板,需要 ...
随机推荐
- mysql远程连接问题 Lost connection to MySQL server at ‘reading initial communication packet', system error: 0
在用Navicat for MySQL远程连接mysql的时候,出现了 Lost connection to MySQL server at ‘reading initial communicatio ...
- JavaWeb xss攻击
出处: http://www.cnblogs.com/TankXiao/archive/2012/03/21/2337194.html XSS 全称(Cross Site Scripting) 跨站脚 ...
- linux 访问 windows 共享文件夹
http://www.01happy.com/linux-access-windows-shares-folders/
- 几种常见的排序算法Java实现总结
public class MySort { final int MAX=20; int num[]=new int[MAX]; { System.out.print("生成的随机数组是:&q ...
- Murano Weekly Meeting 2016.07.26
Meeting time: 2016.July.26 1:00~2:00 Chairperson: Nikolay_St, from Mirantis Meeting summary: 1.Masc ...
- CentOS 7 更新软件源和系统
[1] 首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/Cent ...
- maven课程 项目管理利器-maven 3-7 maven依赖范围 2星
本节主要讲了maven的依赖范围: 在pom.xml dependency标签的scope中.eclipse中有编译的路径,maven中有编译,运行,测试的路径. 1 scope为test,为测试 ...
- Bootstrap学习笔记(四)
四.JS插件 概述:与jQueryUI库类似,Bootstrap提供了十几个插件函数.有两种调用方法: (1) 传统JS变成方式 $(...).插件函数.(); (2) 使用data-*扩展属性(推荐 ...
- staticmethod classmethod property方法
@staticmethod 静态方法 函数修饰符,用来修饰一个函数,类似于装饰器 class Dog(object): def __init__(self,name): self.name = nam ...
- Azure 本月最新活动,速度Mark!
很多时候,为了知晓 Azure 相关活动的信息,需要到处查阅.问朋友同事,这样既麻烦又易造成延误.为方便广大粉丝,我们推出每月活动合集,帮您第一时间了解 Azure 最新活动,还等什么,一起来看吧! ...