传送门: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. tomcat配置访问项目时不需要加项目名称

    原文:http://blog.csdn.net/coolcoffee168/article/details/52582770 java web部署后,访问项目的时候,需要在地址中添加项目名称,那么如何 ...

  2. jmeter的线程组执行顺序不以其出现的顺序发生变化

    jmeter可以同时配置多个线程组,那么他们的执行顺序是什么呢?和他们出现的顺序有什么关系呢? 先说下几个特殊的线程组:tearDown线程组和setUp线程组,tearDown线程组一定在最后执行, ...

  3. jenkins节约硬盘空间的几个办法

    jenkins真是费硬盘和内存,我们先聊聊硬盘问题怎么解决: 1.不要保留太多的构建记录.发布包数量 相关描述如下:取最先匹配进行执行 2.构建完,删除吧

  4. ASP.net MVC+ViewData VS ViewBag

         在使用MVC框架的过程中,往界面传值,我们使用的ViewData.如ITOO部分代码图解:      当然除了ViewData,我们还能够使用同卵兄弟(ViewBag)来完毕相同的功能,详情 ...

  5. oracle 正则查询json返回报文中某个字段的值

    接口返回报文为json 格式,如下: {"body":{"businessinfo":{"c1rate":"25.00" ...

  6. 【转载】TCP和TCP/IP的区别

    TCP/IP协议(Transmission Control Protocol/Internet Protocol)叫做传输控制/网际协议, 又叫网络通讯协议,这个协议是Internet国际互联网络的基 ...

  7. uml时序图的初印象-------Day64

    近期有好多想法迫不及待的想去实现,但是其实是在那些最開始想的很明确,感觉会没问题的地方也总是会出现故障,导致稍微有些急躁,还是要淡定啊.又到了周末.明后天要收拾东西搬家,不知道宽带能不能顺利的给挪过去 ...

  8. CASE UPDATE

    https://leetcode-cn.com/problems/swap-salary/description/ Given a table salary, such as the one belo ...

  9. 20170212-备份ABAP程序

    把生产机上所有后续开发的CBO程序都备份下来.以备急用! 用过2种方法:1.写BDC程序,模拟 TCODE:SE38 -->Program --> Utilities(M)-->Mo ...

  10. bzoj4811 [Ynoi2017]由乃的OJ 树链剖分+位运算

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4811 因为位运算的结果有可合并性,所以可以树链剖分,线段树维护: 细节很多,特别要注意从左往 ...