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 问题描述 ...
随机推荐
- Proxy 动态代理 InvocationHandler CGLIB MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- C指针原理(14)
tcc源码分析 本博客所有内容是原创,如果转载请注明来源 http://blog.csdn.net/myhaspl/ tcctok.h定义了C语言的词法分析的基本元素,主要定义了关键字. /* key ...
- CSS命名规范和规则
一.命名规则 ).尽量不缩写,除非一看就明白的单词 二.class的命名 (1).red { color: red; } .f60 {color: #f60; } .ff8600{ color: #f ...
- 关于 stl的内存分配的深浅拷贝
http://blog.csdn.net/u012501459/article/details/44132147
- Windows server 2008 R2如何预览图片而不是显示图标?
Previews of media files are disabled by default in Windows Server 2008. In this article we will en ...
- OpenGL book list
From: https://www.codeproject.com/Articles/771225/Learning-Modern-OpenGL A little guide about mo ...
- linux 添加elasticsearch 开机重启(自启动)
在 /etc/init.d 文件夹下建立脚本 eg:data.sh #chkconfig: 2345 80 90#description:auto_run#!bin/bashexport JAVA_H ...
- Asp.NET websocket,Asp.NET MVC 使用 SignalR 实现推送功能一(Hubs 在线聊天室)
ASP .NET SignalR 是一个ASP .NET 下的类库,可以在ASP .NET 的Web项目中实现实时通信.什么是实时通信的Web呢?就是让客户端(Web页面)和服务器端可以互相通知消息及 ...
- mysql insert 主键 重复问题
转自:http://blog.163.com/liuweiyoung@126/blog/static/173131045201222122732435/ mysql中insert into和repla ...
- Mat类具体解释(二)
Mat::~Mat Mat的析构函数. C++: Mat::~Mat() 析构函数调用Mat::release(). Mat::operator = 提供矩阵赋值操作. C++: Mat& M ...