- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个有趣的问题困扰了我一段时间。是《算法导论》一书中动态规划的习题。
The telephone company that you are working for has recently taken over the telephone servicesin a new city. You have been assigned specifically to work on the telephone poles on Main street.There are N poles in a row from positions 1 to N, and pole i has height H[i] feet, which is an integer in the range [1,maxH].The city has asked you to make all poles have the same height. For each i, if the i-th pole has height h and the (i−1)-th pole has height h′, then you must pay a tax of C|h−h′|. To help achieve this goal, you can increase the height or decrease the height of any pole to height h with a cost of (H[i]−h)^2. Your task is to decide how to increase or decrease the height of each pole so thatyour company will spend the least amount of money. In particular, you must balance the cost of changing the heights of the poles, with the tax your company will have to pay.
(Hint) you should at least be able do it in O(NH^2), but the best you can do is actually O(HN).
到目前为止,我得到了 O(NH^2)。我的想法是我们将初始化一个大小为 HxN 的矩阵 M,每个条目 M[h,i] 表示第 i 个极点高度为 h 的第 i 个极点的最小成本。我的目标是填充整个矩阵并检查最后一列,即确定最后一个极点的高度对我们的问题具有全局最小成本。
def DP_pole(H,hmax,C,N):
initialize empty matrix M[H,N]
for i from 1 to hmax: #first pole,no tax, only construction fee
M[i,1] = (H[1] - i)**2
for n from 2 to n: #column first
for h from 1 to hmax: #row second
construction = (H[n]-h)**2 # you pay this construction always
minimum = min(M[1,n-1]+C|h-1|,......,M[hmax,n-1]+C|h-hmax|)
M[h,n] = construction + minimum
#------find minimum------#
min = float("-inf")
for h from 1 to hmax:
if M[h,N] < min:
min = M[h,N]
return min
但我认为到目前为止我得到的算法不能简化为 O(HN)(也许?)。因为我使用的递归关系是“线性”关系,为了确定矩阵 H 的每个条目,我将搜索整个前一列,这需要 O(H)。矩阵中总共有H*N项需要填写。
任何提示或帮助将不胜感激。我是否需要想出一个不同的、更聪明的递归关系?
最佳答案
你有正确的动态规划方法,但是对每个 h
执行这个 O(hmax) 操作太昂贵了:
minimum = min(M[1,n-1]+C|h-1|,......,M[hmax,n-1]+C|h-hmax|)
通过一些预处理,您可以在恒定时间内为每个 h
计算此值。
首先,考虑对高度 <= 前一根杆子的新杆子征税。让:
upmin[h] = min(M[h,n-1],......,M[hmax,n-1]+C(hmax-h))
这删除了绝对值运算,这让您可以在 O(hmax) 时间内计算整个数组,因为:
if (h == hmax)
upmin[h] = M[h,n-1]
else
upmin[h] = min( M[h,n-1], upmin[h+1]+C )
您可以在 O(hmax) 中从 hmax
向下计算到 0 来计算此数组。
同样,我们可以考虑对高度 >= 前一根的杆子征税。让:
downmin[h] = min(M[1,n-1]+C(h-1),......,M[h,n-1])
您可以在 O(hmax) 中从 0 向上计算此数组 hmax
完成这些计算后,对于每个 h
,minimum = min(upmin[h],downmin[h])
,当然你可以在常数时间内完成.
关于algorithm - 如何让这个 DP 在 O(NH) 中运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58609288/
我学过这两种DP方式,现在很迷茫。不同情况下我们如何选择?而且我发现在大多数情况下,自上而下对我来说更自然。谁能告诉我如何做出选择。 PS:这篇文章我看过older post但仍然感到困惑。需要帮忙。
我在想是否有某种通用的方法可以将自上而下的动态编程转换为自下而上的编程。 我们能否想出某种机制来提供正式的方式,通过这种方式我们可以将自上而下的 DP 转换为自下而上的 DP。 注意:我是动态规划的初
在我的 xml 中,我在 TextView 上有这个: android:textSize="18dp"(是的,我知道 dp 不适合文本,但这不是重点) 我还有其他一些以编程方式创建的 TextView
假设我正在为屏幕尺寸等于或大于 600dp 的设备开发不同的布局。我想使用 post android 3.2 资源限定符。我创建了一个名为 layout-sw600dp 的文件夹并将我的布局放在那里,
最近我在 https://leetcode.com/problems/palindrome-partitioning-ii/ 上遇到了这个问题: 给定一个字符串s,划分s使得划分的每个子串都是回文。
我已经尝试使用 admob 大约 4 个小时了,现在真的卡住了。我得到: Not enough space to show ad. Needs 320x50 dp, but only has 288
我正在尝试将可变数量的像素计算为与密度无关的像素,反之亦然。 这个公式 (px to dp): dp = (int)(px/(displayMetrics.densityDpi/160)); 不适用于
我正在从头开始创建一个 HTML 页面,并在其中动态放置值,然后将其显示在 WebView 中。 我正在尝试在 WebView(作为 HTML)中复制 ActionBar(出现在应用程序的其他 Act
我正在使用 the 4.17.37 of Bootstrap 3 Datepicker - eonasdan datepicker 我有一个正确显示的内联日期选择器,我只会使用天模式,所以我使用以下代
我正在尝试将智能横幅广告添加到我的 fragment 布局中,但我不断得到的是“没有足够的空间来展示广告。需要 320x50 dp,但只有 309x0 dp”。 这是我的 fragment 布局。
我在关于 http://developer.android.com/guide/practices/screens_support.html 的一些不幸的歧义中迷失了方向,而且我找不到澄清问题的 st
写在前面 $ DP $,是每个信息学竞赛选手所必会的算法,而 $ DP $ 中状态的转移又显得尤为关键。本文主要从状态的设计和转移入手,利用各种方法对朴素 $ DP $ 的时间复杂度和空间复杂度进行
我试图理解一种著名的正则表达式匹配 DP 算法。以防万一,人们不知道这是描述和算法。 '.' Matches any single character. '*' Matches zero or mor
注意:是的,我知道Android中还有其他处理按钮的方法,但这只是一个示例来说明我的问题(实际的按钮要复杂得多)。因此,请不要在为Android中的按钮提供其他解决方案时回信,我正在寻找PaintCo
Closed. This question needs to be more focused。它当前不接受答案。 想改善这个问题吗?更新问题,使其仅关注editing this post的一个问题。
我正在测试 Samsung Galaxy Note 3根据文档,它是 1080 x 1920 像素(~386 ppi 像素密度)。我想弄清楚 dp 中的屏幕宽度 是多少,以便我可以正确应用并检查 ne
所以我现在完全迷路了,当用户通过Accessibility -> Font size 将字体大小时设置为Huge 时,文本大小会破坏我的 UI,所以为了快速修复 我决定将所有文本大小更改为 dp 而不
更新:我发现了我的问题。我已经为不同的分辨率定义了单独的可绘制资源,但我不知道我必须为不同的屏幕尺寸创建不同的布局。我想我应该更全面地阅读“支持多屏幕”页面。遇到此问题的任何其他人,请阅读整个页面:
阅读这篇文章(http://developer.android.com/guide/practices/screens_support.html)后,我在 xml 文件中使用 dp 单元开发了一个完整
该问题需要计算特定成本的硬币变化次数。 例如,如果我有 50, 20, 10, 5, 1 的硬币值(value),我可以形成以下成本: 5 => (5), (11111), 这是2种方式。 10 =>
我是一名优秀的程序员,十分优秀!