此题解法:动态规划,倒骑毛驴。

在使用动态规划的时候,如果正着求难求,可以考虑倒着来。

这道题坑不少,自己代码能力太弱了,写代码的过程中总是容易犯细节错误。虽然大的方向是对的,但是小坑非常致命!

比如一开始下面这两句话写反了。

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周的更多相关文章

  1. hihoCoder 第136周 优化延迟(二分答案+手写堆)

    题目1 : 优化延迟 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho编写了一个处理数据包的程序.程序的输入是一个包含N个数据包的序列.每个数据包根据其重要程度不同 ...

  2. HihoCoder第三周与POJ2406:KMP算法总结

    HihoCoder第三周: 输入 第一行一个整数N,表示测试数据组数. 接下来的N*2行,每两行表示一个测试数据.在每一个测试数据中,第一行为模式串,由不超过10^4个大写字母组成,第二行为原串,由不 ...

  3. hihocoder第42周 3*N骨牌覆盖(状态dp+矩阵快速幂)

    http://hihocoder.com/contest/hiho42/problem/1 给定一个n,问我们3*n的矩阵有多少种覆盖的方法 第41周做的骨牌覆盖是2*n的,状态转移方程是dp[i] ...

  4. hihocoder第42周 k*N骨牌覆盖(状态dp+矩阵快速幂)

    上周的3*N的骨牌,因为状态只有8中,所以我们可以手算出状态转移的矩阵 但是这周是k*N,状态矩阵不好手算,都是我们改成用程序自动生成一个状态转移的矩阵就行了,然后用这个矩阵进行快速幂即可 枚举枚举上 ...

  5. 【hihoCoder 第133周】2-SAT·hihoCoder音乐节

    http://hihocoder.com/contest/hiho133/problem/1 2-sat模板...详细的题解请看题目里的提示. tarjan模板打错again致命伤qwq #inclu ...

  6. hihocoder第220周-一道拧巴的题

    一.220周 题目链接 问题描述 键盘上有N个数字按键,每个按键只能按一次,每次可以按下多个键,请输出所有可能的按键情况. 输入一个整数N(N在1~8之间),输出全部的按键可能.例如:输入3,输出为 ...

  7. 【hihoCoder 第133周】【hihoCoder 1467】2-SAT·hihoCoder音乐节

    http://hihocoder.com/problemset/problem/1467 2-sat模板...详细的题解请看题目里的提示. tarjan模板打错again致命伤qwq #include ...

  8. hihocoder(第十周)二叉树(前序中序推后续)递推实现

    题目 : 后序遍历 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在参与过了美食节之后,小Hi和小Ho在别的地方又玩耍了一阵子,在这个过程中,小Ho得到了一个非常有意思 ...

  9. HihoCoder第五周:标准动态规划

    这周的题目是最标准最简单的动态规划了,自己一直以来对动态规划都不是很理解,这次也是好好记录一下. 题目1 :数字三角形 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 问题描述 ...

随机推荐

  1. 遛老虎网 http://6laohu.com/

    遛老虎网 http://6laohu.com/

  2. impala-shell常用命令

    1.查看相关内网IP: cat /etc/hosts 2.进入impala: impala-shell; 3.显示数据库,数据表 show databases: show tables; 4.查看表结 ...

  3. [javase学习笔记]-6.4 成员变量与局部变量

    前面我们学习了类的定义,我们不难理解,定义类事实上就是在定义类中的成员. 成员包含成员变量和成员函数. 说到成员变量,我们非常自然会想到前面提到过的局部变量,那么它们之间有什么差别呢? 首先我们定义一 ...

  4. 理解MySQL数据库覆盖索引 (转)

    http://www.cnblogs.com/zl0372/articles/mysql_32.html 话说有这么一个表: CREATE TABLE `user_group` ( `id` int( ...

  5. 详解管理root用户权限的sudo服务程序

    在你想要使用超级权限临时运行一条命令时,sudo 命令非常方便,但是当它不能如你期望的工作时,你也会遇到一些麻烦.比如说你想在某些日志文件结尾添加一些重要的信息,你可能会尝试这样做: $ echo & ...

  6. Get file extention in XSLT

      When working with data view web parts or data form web parts in SharePoint, you might want to use ...

  7. $(...).modal is not a function

    bootstrap中调用$(...).modal 方法 提示一个错误 is not a function 检查HTML结构发现,是因为使用了多个Jquery版本,产生了冲突. 解决办法:删掉一个Jqu ...

  8. C++ REST SDK i

    Welcome! The C++ REST SDK is a Microsoft project for cloud-based client-server communication in nati ...

  9. MDX Step by Step 读书笔记(八) - Navigating Hierarchies 层次结构导航

    开篇介绍 本章主要内容包括: 解释各种不同的 MDX 导航函数的使用: Parent, Children, FirstChild, LastChild, Siblings, FirstSibling, ...

  10. 高级NUMA参数

    Advanced NUMA Attributes You can use the advanced NUMA attributes to customize NUMA usage. Attribute ...