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

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

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

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

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. python2.7 调用 .net的webservice asmx

    首先安装pip install suds 或下载:https://pypi.org/project/suds-jurko/0.6/#files 这个是最新版本 由于不支持python3.6, 所以只能 ...

  2. linux里tmpfs文件系统

    linux里tmpfs文件系统 是一个虚拟内存文件系统,它不同于传统的用块设备形式来实现的Ramdisk,也不同于针对物理内存的Ramfs.Tmpfs可以使用物理内存,也可以使用交换分区. umoun ...

  3. (转)AssetBundle系列——共享资源打包/依赖资源打包

    有人在之前的博客中问我有关共享资源打包的代码,其实这一块很简单,就两个函数: BuildPipeline.PushAssetDependencies():依赖资源压栈: BuildPipeline.P ...

  4. Angular 2的12个经典面试问题汇总(文末附带Angular測试)

    Angular作为眼下最为流行的前端框架,受到了前端开发者的普遍欢迎.不论是初学Angular的新手.还是有一定Angular开发经验的开发者,了解本文中的12个经典面试问题,都将会是一个深入了解和学 ...

  5. Shader开发工具: PVRShaman

    1. A PVRShaman (.POD) workspace is provided as an example in the Chapter_10/PVR_LinearFog folder tha ...

  6. 解决webstorm本地IP访问页面出错的问题,webstorm支持IP访问

    想在手机端访问webstorm做出的页面,遇到了根据IP地址访问页面错误的问题,试了网上的方法:“设置webstorm可以被外部连接访问”,依旧不能解决 解决方法: 在webstorm下:ctrl+a ...

  7. C#.NET常见问题(FAQ)-命名空间namespace如何理解

    Namespace就是定义一个对象是哪个门派的,比如你现在就在A门派下,A门派的所有功夫(属性和方法)都可以不加声明直接调用.如果你要用B门派的功夫,就要先声明是B门派的XXX对象的XXX实例的XXX ...

  8. Linux网络编程:基于UDP的程序开发回顾篇

    基于无连接的UDP程序设计 同样,在开发基于UDP的应用程序时,其主要流程如下:   对于面向无连接的UDP应用程序在开发过程中服务端和客户端的操作流程基本差不多.对比面向连接的TCP程序,服务端少了 ...

  9. 2014秋C++ 第9周项目 循环程序设计

    课程主页在http://blog.csdn.net/sxhelijian/article/details/39152703.课程资源在云学堂"贺老师课堂"同步展示,使用的帐号请到课 ...

  10. thinkphp3错误:syntax error, unexpected 'list' (T_LIST), expecting identifier (T_STRING)

    syntax error, unexpected 'list' (T_LIST), expecting identifier (T_STRING) 出现这个错误的原因是,list是php的一个函数,系 ...