[POJ3253]Fence Repair(单调队列)
题目链接
http://poj.org/problem?id=3253
题目描述
大意:切长度为a的木条的花费是a,给定最终切好的n段各自的长度,问由原来的一根木条(长度为n段长度和)以最终总花费最小的方法切这n-1下,输出最小花费。(切的中间过程产生的子段不同,所以花费会不同)
例子:
输入:
3
8 5 8
输出:
34
提示:
(8+5)+(13+8)=34
题解
- 过程反着想,就是合并子段的过程。
- 数据结构:两个单调递增队列。
- 第一个放进原始的n段的长度。
- 第二个用来放合并后的段的长度。
- 每次贪心地选择两个队列头最小的两个段合并。合并后放入队列2头部,可以知道队列2也是单调递增的。
代码
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n=in.nextInt();
LinkedList<Integer> q=new LinkedList<Integer>();
while(n--!=0) {
q.addLast(in.nextInt());
}
int cost=getMinCost(q);
System.out.println(cost);
}
public static int getMinCost(LinkedList<Integer> q) {
q.sort(null);
int n=q.size()-1;//切(合并)的次数
LinkedList<Integer> sumQue=new LinkedList<>();
int sum=0;
while(n--!=0) {
int tempSum=0;
int cnt=2;
while(cnt--!=0) {
if(sumQue.isEmpty()||(!sumQue.isEmpty()&&!q.isEmpty()&&q.peekFirst()<=sumQue.peekFirst())) {
tempSum+=q.pollFirst();
}
else {
tempSum+=sumQue.pollFirst();
}
}
sum+=tempSum;
sumQue.addFirst(tempSum);
}
return sum;
}
}
[POJ3253]Fence Repair(单调队列)的更多相关文章
- 优先队列 poj3253 Fence Repair
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 51411 Accepted: 16879 De ...
- poj 1821 Fence(单调队列优化DP)
poj 1821 Fence \(solution:\) 这道题因为每一个粉刷的人都有一块"必刷的木板",所以可以预见我们的最终方案里的粉刷匠一定是按其必刷的木板的顺序排列的.这就 ...
- [POJ1821]Fence(单调队列优化dp)
[poj1821]Fence 有 N 块木板从左至右排成一行,有 M 个工匠对这些木板进行粉刷,每块木板至多被粉刷一次.第 i 个工匠要么不粉刷,要么粉刷包含木板 Si 的,长度不超过Li 的连续一段 ...
- fence repair(队列水过)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32916 Accepted: 10638 点我 ...
- Poj3253 Fence Repair (优先队列)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 67319 Accepted: 22142 De ...
- poj1821 Fence(dp,单调队列优化)
题意: 由k(1 <= K <= 100)个工人组成的团队应油漆围墙,其中包含N(1 <= N <= 16 000)个从左到右从1到N编号的木板.每个工人i(1 <= i ...
- poj3253 Fence Repair
http://poj.org/problem?id=3253 Farmer John wants to repair a small length of the fence around the pa ...
- poj-3253 fence repair(贪心题)
题目描述: Farmer John wants to repair a small length of the fence around the pasture. He measures the fe ...
- poj3253 Fence Repair【哈夫曼树+优先队列】
Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...
随机推荐
- ElasticSearch 7.8.1集群搭建
通往集群的大门 集群由什么用? 高可用 高可用(High Availability)是分布式系统架构设计中必须考虑的因素之一,它通常是指,通过设计减少系统不能提供服务的时间.如果系统每运行100个时间 ...
- Spring Security报异常 Encoded password does not look like BCrypt
控制台报错: Encoded password does not look like BCrypt 意思是前端传回去的密码格式与数据库里的密码格式不匹配,这样即使密码正确也无法校验.自然也就无法登录. ...
- Python方法oslo_service.loopingcall.LoopingCallDone代码示例
Python方法oslo_service.loopingcall.LoopingCallDone代码示例 demo: from oslo_service import loopingcall def ...
- centos iso镜像自动挂载
vim /etc/fstab 添加如下行:/usr/ison/centos.iso /media/cdrom iso9660 defaults,loop 0 0
- Linux的CentOs系统查看CPU个数、核心数、线程数
1.查看CPU物理个数 grep 'physical id' /proc/cpuinfo | sort -u | wc -l 或 cat /proc/cpuinfo| grep "physi ...
- Linux下安装mysql时报错:FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:Data::Dumper
如题,安装mysql过程中,执行scripts/mysql_install_db --user=mysql命令时报错: FATAL ERROR: please install the followin ...
- 【转】Python进度条tqdm的使用
有时候在使用Python处理比较耗时操作的时候,为了便于观察处理进度,这时候就需要通过进度条将处理情况进行可视化展示,以便我们能够及时了解情况.这对于第三方库非常丰富的Python来说,想要实现这一功 ...
- CSS动画实例:3D立方体
CSS3支持3D转换,与3D转换有关的属性有: transform:向元素应用 2D或3D 转换. transform-origin:改变被转换元素的位置. transform-style:规定被嵌套 ...
- ent orm笔记1---快速尝鲜
前几天看到消息Facebook孵化的ORM ent转为正式项目,出去好奇,简单体验了一下,使用上自己感觉比GORM好用,于是打算把官方的文档进行整理,也算是学习一下如何使用. 安装 ent orm 需 ...
- akka-grpc - 应用案例
上期说道:http/2还属于一种不算普及的技术协议,可能目前只适合用于内部系统集成,现在开始大面积介入可能为时尚早.不过有些项目需求不等人,需要使用这项技术,所以研究了一下akka-grpc,写了一篇 ...