[BZOJ 1724] Fence Repair
这大概是BZOJ里除了A+B Problem最水的一道题了吧
题面:http://www.lydsy.com/JudgeOnline/problem.php?id=1724
这道题其实有一些思路还是可以借鉴的
首先是逆向思维:为了让计算次数最多的边必须是最小的边,因此最小的和倒数第二小的必然在二叉树的最低端
接下来是递归(递推)的思想,将二者合并后,此问题又变为和合并前完全相同的问题,递推解决即可
维护自然就用priority_queue
#include <bits/stdc++.h> using namespace std;
typedef long long ll;
int n;
priority_queue<int,vector<int>,greater<int> > q; inline int read()
{
char ch;
int num,f=;
while(!isdigit(ch=getchar())) f|=(num=='-');
num=ch-'';
while(isdigit(ch=getchar())) num=num*+ch-'';
return f?-num:num;
} int main()
{
n=read();
for(int i=;i<=n;i++)
{
int x=read();
q.push(x);
} if(n==){cout << q.top();return ;} ll res=;
while(q.size()>)
{
int x=q.top();q.pop();
int y=q.top();q.pop();
res+=(x+y);
q.push(x+y);
}
cout << res;
return ;
}
[BZOJ 1724] Fence Repair的更多相关文章
- BZOJ 3253 Fence Repair 哈夫曼树 水题
http://poj.org/problem?id=3253 这道题约等于合并果子,但是通过这道题能够看出来哈夫曼树是什么了. #include<cstdio> #include<c ...
- BZOJ 1724: [Usaco2006 Nov]Fence Repair 切割木板
题目 1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec Memory Limit: 64 MB Description Farmer ...
- 1724: [Usaco2006 Nov]Fence Repair 切割木板( 贪心 )
倒过来看 , 每次总是选择最短的两块木板合并 , 用heap维护 ------------------------------------------------------------------- ...
- 1724: [Usaco2006 Nov]Fence Repair 切割木板
1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 854 Solved: 42 ...
- poj 3253 Fence Repair
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42979 Accepted: 13999 De ...
- Greedy:Fence Repair(POJ 3252)
Fence Repair 问题大意:农夫约翰为了修理栅栏,要将一块很长的木块切割成N块,准备切成的木板的长度为L1,L2...LN,未切割前的木板的长度恰好为切割后木板的长度的总和,每次切断木板的时候 ...
- poj 3253:Fence Repair(堆排序应用)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 23913 Accepted: 7595 Des ...
- 哈夫曼树-Fence Repair 分类: 树 POJ 2015-08-05 21:25 2人阅读 评论(0) 收藏
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32424 Accepted: 10417 Descri ...
- POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
随机推荐
- 20151024_002_C#基础知识(ArrayList,Hashtable,List,Dictionary)
1:ArrayList 和 Hashtable(哈希表) 1.1:ArrayList ArrayList list = new ArrayList(); list.Add(); list.AddRan ...
- Edgware Feign hystrix-dashboard
相关依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring ...
- 宿主机mount虚拟机镜像文件
转载 mount挂载虚拟机镜像文件 使用mount挂载ubuntu虚拟机所在的img文件的时候,执行: “sudo mount -o loop xxx.img /mnt/xxx”, 系统提示: “mo ...
- Vue优化首屏加载
背景: 使用vue + iview搭建的一个后台管理系统,路由已经用了懒加载,加载登陆页面,居然还是需要18S左右,刚到一个新公司,项目经理很委婉的说,看看能不能优化了一下.然后就开始了网上一大堆'v ...
- redis的安装和php的redis扩展
一.redis的安装和配置 1.官方现在源码 https://redis.io/download 2.解压源码 tar zxvf redis-3.2.11.tar.gz 3.编译 make 编译 ...
- 18:django 日志系统
django使用python内建的logging模块去建造自己的系统日志的,如果你想详细了解这个模块的话,请自己去看python的说明文档,这里仅仅介绍django中的日志系统 日志配置包括四个部分: ...
- linux命令(7):ipcs/ipcrm命令
ipcs作用 :查看消息队列(ipcs –q).共享内存(ipcs –m).信号灯(ipcs -s) ipcrm作用 :删除消息队列.共享内存.信号灯 ipcrm使用方式: ipcrm [ -M ke ...
- ZOJ-3319
Islands Time Limit: 1 Second Memory Limit: 32768 KB There are N islands and some directed paths ...
- sharding-jdbc 实现分表
Sharding-JDBC 简介 Sharding-JDBC直接封装JDBC API,可以理解为增强版的JDBC驱动,旧代码迁移成本: 可适用于任何基于Java的ORM框架,如:JPA.HIberna ...
- js 获取html5的data属性
我以前一直以为只能用jquery的data()来获取 哈哈 是我太弱了 <!DOCTYPE html> <html> <head> <title>dat ...