- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
假设我们有一个长度为 n 的序列。这个序列中的每个数字都有一个权重。我们想用动态规划找到长度为 3 的最小加权递增子序列。我们该怎么做?
例子:
序列:2 4 5 4 10
重量:40 30 20 10 40
答案是 90 ( 2 4 10)
最佳答案
我在一些 codeforces 回合中解决了这个完全相同的问题。 ( http://codeforces.com/contest/987/problem/C )
我的解决方案是 O(N^2),其中 N 是房屋的数量。
它的工作原理如下,对于每间房子 k,我将搜索属于 K 右边的最便宜的房子。
一旦我有了这些信息,我就尝试所有可能的对 (i, j) 使得 height[i] < height[j] 并将比位置 j 处的房子高的最便宜的房子加到这一对上。
这是我的代码!
#include <bits/stdc++.h>
using namespace std;
constexpr int inf = 0x3f3f3f3f;
int main()
{
int n;
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
vector<int> height(n);
for(int i = 0; i < n; ++i) cin >> height[i];
vector<int> cost(n);
for(int i = 0; i < n; ++i) cin >> cost[i];
vector<int> cheapestToTheRight(n, inf);
cheapestToTheRight[n-1] = inf;
for(int k = n - 2; k >= 0; --k)
{
for(int nxt = k + 1; nxt < n; ++nxt)
{
if(height[nxt] > height[k])
{
cheapestToTheRight[k] = min(cheapestToTheRight[k], cost[nxt]);
}
}
}
int ans = inf;
for(int i = 0; i < n; ++i)
{
for(int j = i + 1; j < n; ++j)
{
if(height[i] < height[j])
{
if(cheapestToTheRight[j] != inf)
{
ans = min( ans, cost[i] + cost[j] + cheapestToTheRight[j]);
}
}
}
}
if(ans == inf)
{
cout << -1 << endl;
}
else cout << ans << endl;
return 0;
}
这是我提交的链接 ( http://codeforces.com/contest/987/submission/38734180 ),但代码比较脏,上面有一些葡萄牙语变量名。
关于寻找长度为 3 的递增序列的最低成本的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50784156/
题: 是否有一种简单的方法可以获取正在运行的应用程序中泄漏的资源类型列表? IOW 通过连接到应用程序? 我知道 memproof 可以做到,但它会减慢速度,以至于应用程序甚至无法持续一分钟。大多数任
正确地说下面的代码会将自定义日志发送到.net核心中的Docker容器的stdout和stderr吗? console.Writeline(...) console.error(..) 最佳答案 如果
我想将一个任务多次重复,放入 for 循环中。我必须将时间序列对象存储为 IExchangeItem , openDA 中的一个特殊类(数据同化软件)。 这是任务之一(有效): HashMap ite
我需要从文件中读取一个数组。该数组在文件中不是连续排序的,必须跳转“偏移”字节才能获得下一个元素。假设我读取一个非常大的文件,什么更有效率。 1) 使用增量相对位置。 2)使用绝对位置。 选项 1:
我有一个安装程序(使用 Advanced Installer 制作)。我有一个必须与之交互的应用程序,但我不知道如何找到该安装的 MSIHANDLE。我查看了 Microsoft 引用资料,但没有发现
我在替换正则表达式中的“joe.”等内容时遇到问题。这是代码 var objects = new Array("joe","sam"); code = "joe.id was here so was
我有 A 类。A 类负责管理 B 对象的生命周期,它包含 B 对象的容器,即 map。 ,每个 B 对象都包含 C 对象的容器,即 map .我有一个全局 A 对象用于整个应用程序。 我有以下问题:我
任何人都可以告诉我在哪里可以找到 freeImage.so 吗?我一直在努力寻找相同的东西但没有成功..任何帮助将不胜感激。我已经尝试将 freeimage.a 转换为 freeImage .so 并
在单元测试期间,我想将生成的 URL 与测试中定义的静态 URL 进行比较。对于此比较,最好有一个 TestCase.assertURLEqual 或类似的,它可以让您比较两个字符串格式的 URL,如
'find ./ -name *.jpg' 我正在尝试优化上述语句的“查找”命令。 在查找实现中处理“-name”谓词的方法。 static boolean pred__name __common (
请原谅我在这里的困惑,但我已经阅读了关于 python 中的 seek() 函数的文档(在不得不使用它之后),虽然它帮助了我,但我仍然对它的实际含义有点困惑,任何非常感谢您的解释,谢谢。 最佳答案 关
我在我正在使用的库中找到了这个语句。它应该检查集群中的当前节点是否是领导者。这是语句:(!(cluster.Leader?.IsRemote ?? true)) 为什么不直接使用 (cluster.L
我发现 JsonParser 在 javax.json.stream 中,但我不知道在哪里可以找到它。谁能帮帮我? https://docs.oracle.com/javaee/7/api/javax
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 6年前关闭。 Improve this questi
如果 git 存储库中有新的更改可用,我有一个多分支管道作业设置为每分钟由 Jenkinsfile 构建。如果分支名称是某种格式,我有一个将工件部署到环境的步骤。我希望能够在每个分支的基础上配置环境,
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
我想我刚刚意识到当他们不让我使用 cfdump 时我的网络主机是多么的限制。这其实有点让我生气,真的,dump 有什么害处?无论如何,我的问题是是否有人编写了一个 cfdump 替代方案来剔除复杂类型
任务:我有多个资源需要在一个 HTTP 调用中更新。 要更新的资源类型、字段和值对于所有资源都是相同的。 示例:通过 ID 设置了一组汽车,需要将所有汽车的“状态”更新为“已售出”。 经典 RESTF
场景:表中有 2 列,数据如下例所示。对于“a”列的相同值,该表可能有多个行。 在示例中,考虑到“a”列,“1”有三行,“2”有一行。 示例表“t1”: |a|b ||1|1.1||1|1.2||1
我有一个数据框: Date Price 2021-01-01 29344.67 2021-01-02 32072.08 2021-01-03 33048.03 2021-01-04 32084.
我是一名优秀的程序员,十分优秀!