传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=126

好题啊好题,一开始就输给了这道题的想法!

先把原始状态以及目标状态换一种表示方式,比如输入数据是的初始状态是1 2 3 4,表示成1 2 2 3 3 3 4 4 4 4,目标状态是4 3 2 0,表示成1 1 1 1 2 2 2 3 3。那么这题就是求把原始状态转化为目标状态的最小代价,只有三种操作, add, remove, transport,所以这就是一个求Edit Distance(编辑距离)的裸题!令f(i, j)表示初始状态的前i个转化为目标状态的前j个所需最小代价,则

f(i, j) = min(  f(i - 1, j) + y,     f(i, j - 1) + x,    f(i - 1, j - 1) + z * abs(a[i] - b[j])   )

其中a[i]表示初始状态的第i个是什么,同理b[j]。

#include <cstdio>
#include <algorithm>
#include <cstring> const int maxn = 105; int n, a[1005], b[1005], idxa, idxb, f[1005][1005], x, y, z, t; int main(void) {
freopen("landscape.in", "r", stdin);
freopen("landscape.out", "w", stdout);
scanf("%d%d%d%d", &n, &x, &y, &z);
memset(f, 0x3c, sizeof f);
for (int i = 1; i <= n; ++i) {
scanf("%d", &t);
while (t--) {
a[++idxa] = i;
}
scanf("%d", &t);
while (t--) {
b[++idxb] = i;
}
} for (int i = 1; i <= idxa; ++i) {
f[i][0] = i * y;
}
for (int j = 1; j <= idxb; ++j) {
f[0][j] = j * x;
}
f[0][0] = 0;
for (int i = 1; i <= idxa; ++i) {
for (int j = 1; j <= idxb; ++j) {
f[i][j] = std::min(std::min(f[i - 1][j] + y, f[i][j - 1] + x), f[i - 1][j - 1] + z * std::abs(a[i] - b[j]));
}
}
printf("%d\n", f[idxa][idxb]);
return 0;
}

  

[USACO 2012 Mar Silver] Landscaping【Edit Distance】的更多相关文章

  1. [USACO 2012 Open Gold] Bookshelf【优化dp】

    传送门1:http://www.usaco.org/index.php?page=viewproblem2&cpid=138 传送门2:http://www.lydsy.com/JudgeOn ...

  2. 【Edit Distance】cpp

    题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...

  3. USACO翻译:USACO 2012 FEB Silver三题

    USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...

  4. NC24325 [USACO 2012 Mar S]Flowerpot

    NC24325 [USACO 2012 Mar S]Flowerpot 题目 题目描述 Farmer John has been having trouble making his plants gr ...

  5. [USACO 2012 Jan Silver] Bale Share【DP】

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=107 没想到太不应该了,真的不应该啊! f[i][j][k]表示前i个包, ...

  6. [USACO 2012 Jan Silver] Delivery Route【拆点】

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=106 这道题还真是完全没思路,真的不知道怎么做,但是看了题解后恍然大悟. ...

  7. [USACO 2012 Mar Gold] Large Banner

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=127 又是一道这种题目,遇到一次跪一次,这次终于硬着头皮看懂了题解,但是谢 ...

  8. USACO 2008 Mar Silver 3.River Crossing 动态规划水题

    Code: #include<cstring> #include<algorithm> #include<cstdio> using namespace std; ...

  9. USACO 2012 March Silver Tractor /// 优先队列BFS oj21567

    题目大意: 输入n,(x,y):n为阻挡的草堆数量,(x,y)为开始时拖拉机所在的位置 接下来n行每行一个坐标(a,b):为各个草堆的坐标 输出拖拉机要回到原点(0,0)需要移动的草堆数量 Sampl ...

随机推荐

  1. spring理解一

    spring基本工作原理例如以下: 1.查找bean配置文件 2.载入bean配置文件并解析生成中间表示BeanDefinition 3.注冊beanDefinition 4.假设是单例或lazy-i ...

  2. Oracle APEX 4.2安装和配置

    A standard Oracle 11.2.0.3 database installation comes bundled with Application Express (APEX) 3.2.1 ...

  3. 【求建议】毕业之声——信院IT类毕业学子经验分享交流会

    一:缘由 在和非常多学子交流,及上课的经历中,发现一个非常普遍的现象:部分大一学生即失去了对学习.对专业的兴趣.有人在迷茫之后奋起直追.从而珍惜利用不多的大学时光努力提高自己.有人在迷茫中沉沦,沉迷于 ...

  4. Python中的列表,元组,字符串之间的相互转化

    Python中的列表元组和字符串之间的相互转化需要利用,tuple(),list(),str(). 示例如下: >>> the_string = "hello I'am x ...

  5. Kemans算法及其Python 实现

    算法优缺点: 优点:容易实现缺点:可能收敛到局部最小值,在大规模数据集上收敛较慢使用数据类型:数值型数据 算法思想 k-means算法实际上就是通过计算不同样本间的距离来判断他们的相近关系的,相近的就 ...

  6. 解决oracle 表被锁住问题

    想修改Oracle下的某一张表,提示 "资源正忙, 但指定以 NOWAIT 方式获取资源, 或者超时失效" 看上去是锁住了. 用系统管理员登录进数据库,然后 SELECT sid, ...

  7. js连等运算

    1.var a = b = 20; 连等的第二个变量属于全局变量2.a.x = a = {n:2}; 连等是从右往左执行的3.a.x = a = {n:2}; js语句执行前会保存之前的索引

  8. 常用的机器学习&数据挖掘知识点总结

    Basis(基础): MSE(Mean Square Error 均方误差),LMS(LeastMean Square 最小均方),LSM(Least Square Methods 最小二乘法),ML ...

  9. YTU 2405: C语言习题 牛顿迭代法求根

    2405: C语言习题 牛顿迭代法求根 时间限制: 1 Sec  内存限制: 128 MB 提交: 562  解决: 317 题目描述 用牛顿迭代法求根.方程为ax3+bx2+cx+d=0.系数a,b ...

  10. 比特币客户端bitcoind的高级用法

    Bitcoin 比特币官方客户端有两个版本:一个是图形界面的版本,通常被称为 Bitcoin(首字母大写),以及一个简洁命令行的版本(称为 bitcoind).它们相互间是兼容的,有着同样的命令行参数 ...