hihocoder第196周
此题解法:动态规划,倒骑毛驴。
在使用动态规划的时候,如果正着求难求,可以考虑倒着来。
这道题坑不少,自己代码能力太弱了,写代码的过程中总是容易犯细节错误。虽然大的方向是对的,但是小坑非常致命!
比如一开始下面这两句话写反了。
nowHeight = (int) Math.max(nowHeight, Math.ceil(h[i] * (M - y) * 1.0 / w[i]));
y = 0;
import com.sun.rowset.internal.Row;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
int M, N;
int w[], h[];
Node f[][];
class Node {
int rowHeight;//行高
int height;//高度
Node(int rowHeight, int height) {
this.rowHeight = rowHeight;
this.height = height;
}
}
void solve() {
int nowHeight = 0;//行高
int height = 0;//总高
int y = 0;//当前行位置
int ans = N * 107;
for (int i = 0; i < N - 1; i++) {
if (y == 0) {
ans = Math.min(ans, height + f[i + 1][0].height + f[i + 1][0].rowHeight);
} else {
ans = Math.min(ans, height + f[i + 1][y].height + Math.max(f[i + 1][y].rowHeight, nowHeight));
}
if (y + w[i] < M) {
y += w[i];
nowHeight = Math.max(nowHeight, h[i]);
} else if (y + w[i] == M) {
nowHeight = Math.max(h[i], nowHeight);
height += nowHeight;
nowHeight = 0;
y = 0;
} else {
nowHeight = (int) Math.max(nowHeight, Math.ceil(h[i] * (M - y) * 1.0 / w[i]));
height += nowHeight;
nowHeight = 0;
y = 0;
}
}
ans = Math.min(ans, height + nowHeight);
System.out.println(ans);
}
Main() throws FileNotFoundException {
Scanner cin = new Scanner(System.in);
// Scanner cin = new Scanner(new FileInputStream("in.txt"));
M = cin.nextInt();
N = cin.nextInt();
w = new int[N];
h = new int[N];
f = new Node[N + 1][M];
for (int i = 0; i < N; i++) {
w[i] = cin.nextInt();
h[i] = cin.nextInt();
}
cin.close();
for (int j = 0; j < M; j++) {
f[N][j] = new Node(0, 0);
}
for (int i = N - 1; i >= 0; i--) {
for (int j = 0; j < M; j++) {
if (j + w[i] < M) {
Node next = f[i + 1][j + w[i]];
f[i][j] = new Node(Math.max(h[i], next.rowHeight), next.height);
} else if (j + w[i] == M) {
Node next = f[i + 1][0];
f[i][j] = new Node(h[i], next.height + next.rowHeight);
} else {
int myh = (int) Math.ceil(h[i] * 1.0 * (M - j) / w[i]);
Node next = f[i + 1][0];
f[i][j] = new Node(myh, next.height + next.rowHeight);
}
}
}
solve();
}
public static void main(String[] args) throws FileNotFoundException {
new Main();
}
}
hihocoder第196周的更多相关文章
- hihoCoder 第136周 优化延迟(二分答案+手写堆)
题目1 : 优化延迟 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho编写了一个处理数据包的程序.程序的输入是一个包含N个数据包的序列.每个数据包根据其重要程度不同 ...
- HihoCoder第三周与POJ2406:KMP算法总结
HihoCoder第三周: 输入 第一行一个整数N,表示测试数据组数. 接下来的N*2行,每两行表示一个测试数据.在每一个测试数据中,第一行为模式串,由不超过10^4个大写字母组成,第二行为原串,由不 ...
- hihocoder第42周 3*N骨牌覆盖(状态dp+矩阵快速幂)
http://hihocoder.com/contest/hiho42/problem/1 给定一个n,问我们3*n的矩阵有多少种覆盖的方法 第41周做的骨牌覆盖是2*n的,状态转移方程是dp[i] ...
- hihocoder第42周 k*N骨牌覆盖(状态dp+矩阵快速幂)
上周的3*N的骨牌,因为状态只有8中,所以我们可以手算出状态转移的矩阵 但是这周是k*N,状态矩阵不好手算,都是我们改成用程序自动生成一个状态转移的矩阵就行了,然后用这个矩阵进行快速幂即可 枚举枚举上 ...
- 【hihoCoder 第133周】2-SAT·hihoCoder音乐节
http://hihocoder.com/contest/hiho133/problem/1 2-sat模板...详细的题解请看题目里的提示. tarjan模板打错again致命伤qwq #inclu ...
- hihocoder第220周-一道拧巴的题
一.220周 题目链接 问题描述 键盘上有N个数字按键,每个按键只能按一次,每次可以按下多个键,请输出所有可能的按键情况. 输入一个整数N(N在1~8之间),输出全部的按键可能.例如:输入3,输出为 ...
- 【hihoCoder 第133周】【hihoCoder 1467】2-SAT·hihoCoder音乐节
http://hihocoder.com/problemset/problem/1467 2-sat模板...详细的题解请看题目里的提示. tarjan模板打错again致命伤qwq #include ...
- hihocoder(第十周)二叉树(前序中序推后续)递推实现
题目 : 后序遍历 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在参与过了美食节之后,小Hi和小Ho在别的地方又玩耍了一阵子,在这个过程中,小Ho得到了一个非常有意思 ...
- HihoCoder第五周:标准动态规划
这周的题目是最标准最简单的动态规划了,自己一直以来对动态规划都不是很理解,这次也是好好记录一下. 题目1 :数字三角形 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 问题描述 ...
随机推荐
- Java:Linux上java -jar xxx.jar&java -cp 区别
java -cp java -cp 和 -classpath 一样,是指定类运行所依赖其他类的路径,通常是类库和jar包,需要全路径到jar包,多个jar包之间连接符:window上分号“;”.Lin ...
- Python防止sql注入
看了网上文章,说的都挺好的,给cursor.execute传递格式串和参数,就能防止注入,但是我写了代码,却死活跑不通,怀疑自己用了一个假的python 最后,发现原因可能是不同的数据库,对于字符串的 ...
- Mongoose vs mongodb native driver – what to prefer?
Paul Shan 7th Jun 2015 Mongoose or mongodb native driver, which one to use? This is one of the ini ...
- External component has thrown an exception
C#调用c++的DLL报错:External component has thrown an exception, 也没有log产生,怎么回事那? [解决方法] 这是因为c++的程序报错了,而且没有c ...
- jquery旋转图片
今天介绍一款 jQuery 插件——jqueryrotate,它可以实现旋转效果.jqueryrotate 支持所有主流浏览器,包括 IE6.如果你想在低版本的 IE 中实现旋转效果,那么 jquer ...
- Android 显示网络图片
本文内容 环境 演示显示网络图片 本文演示 Android 如何显示网络图片.学习一门新的语言,最好办法就先了解该语言的语法和库,以及设计思想,再着手现实一些常用功能,毕竟以后用该语言是要写程序的,而 ...
- 构建高性能服务(二)java高并发锁的3种实现
构建高性能服务(二)java高并发锁的3种实现 来源:http://www.xymyeah.com/?p=46 提高系统并发吞吐能力是构建高性能服务的重点和难点.通常review代码时看到sync ...
- Springboot项目启动报错,提示Cannot determine embedded database driver class for database type NONE
我在springboot项目里面引入了数据库的配置: <dependency> <groupId>org.mybatis.spring.boot</groupId> ...
- Struts2添加了<s:debug>后页面无效果的解决方案
一.环境 Struts2版本 struts2.5 二.问题 在jsp页面中添加了<s:debug>后页面上无任何展示. 三.解决 在struts.xml中的struts节点下添加如下常量即 ...
- Hotmail Smtp邮箱发送的端口
1.最近有项目需求做监控报警. 2.使用Smtp发邮件时,网上找了一大堆,Smtp服务是:smtp.live.com 端口是:25或587,试了好多次都不行.原来端口是465. 3.发送时,我启用 ...