传送门: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. Shiro经过Redis管理会话实现集群(转载)

    原文:http://www.myexception.cn/software-architecture-design/1815507.html Shiro通过Redis管理会话实现集群 写在前面 1.在 ...

  2. Data obtained from ping: is it round trip or one way?

    I have 2 servers, each in two separate locations. I need to host an application on one, and the data ...

  3. 一起talk C栗子吧(第九十六回:C语言实例--使用共享内存进行进程间通信二)

    各位看官们.大家好,上一回中咱们说的是使用共享内存进行进程间通信的样例,这一回咱们接着上一回内容继续说使用共享内存进行进程间通信. 闲话休提,言归正转.让我们一起talk C栗子吧! 我们在上一回中介 ...

  4. Policy-based design设计模式

    书在4年前看过.今天重温一下: 一直觉得这是最好的设计模式,大牛Andrei Alexandrescu 专门写了书,可见他的重要性 http://en.wikipedia.org/wiki/Polic ...

  5. Oracle 模糊查询方法

           在这个信息量剧增的时代,怎样帮助用户从海量数据中检索到想要的数据.模糊查询是不可缺少的. 那么在Oracle中模糊查询是怎样实现的呢?   一.我们能够在where子句中使用likeke ...

  6. java重载中的基本类型的自动类型转换

    当传递到函数的参数的数据类型表示的范围小于函数形参的参数类型遵循如下原则 : char类型比较特殊, 直接转换为int:  char ->int ->long->float-> ...

  7. 纯JS写的一款记录事项的单页应用

    要点: 1.使用localStorage存储 2._change_record_progress函数以字符串作为参数,用eval执行这个参数 3.使用了jQuery自定义事件,便于数据改变时实时更新显 ...

  8. Navicat for MySQL中文破解版免费下载

    不知道用过MySQL workbench数据库的朋友或站长有没有都遇到过这样的问题? 就是用ssh的连接方式连接数据库的时候,第一次可以连接的上,但是第二次就不行了,以后再用workbench就再也连 ...

  9. 硬件开发之pcb---PCB抗干扰设计原则

    一 电源线布置: 1.电源线.地线的走向应与资料的传递方向一致. 二 地线布置: 1.数字地与模拟地分开. 2.接地线应尽量加粗,致少能通过3倍于印制板上的允许电流,一般应达2~3mm. 3.接地线应 ...

  10. windows下Python扩展问题error: Unable to find vcvarsall.bat

    由于对于Windows下Python扩展不熟,今天遇到一个安装问题,特此做个tag.解决方式在stackoverflow上,网址例如以下: http://stackoverflow.com/quest ...