[USACO 2012 Mar Silver] Landscaping【Edit Distance】
传送门: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】的更多相关文章
- [USACO 2012 Open Gold] Bookshelf【优化dp】
传送门1:http://www.usaco.org/index.php?page=viewproblem2&cpid=138 传送门2:http://www.lydsy.com/JudgeOn ...
- 【Edit Distance】cpp
题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...
- USACO翻译:USACO 2012 FEB Silver三题
USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...
- NC24325 [USACO 2012 Mar S]Flowerpot
NC24325 [USACO 2012 Mar S]Flowerpot 题目 题目描述 Farmer John has been having trouble making his plants gr ...
- [USACO 2012 Jan Silver] Bale Share【DP】
传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=107 没想到太不应该了,真的不应该啊! f[i][j][k]表示前i个包, ...
- [USACO 2012 Jan Silver] Delivery Route【拆点】
传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=106 这道题还真是完全没思路,真的不知道怎么做,但是看了题解后恍然大悟. ...
- [USACO 2012 Mar Gold] Large Banner
传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=127 又是一道这种题目,遇到一次跪一次,这次终于硬着头皮看懂了题解,但是谢 ...
- USACO 2008 Mar Silver 3.River Crossing 动态规划水题
Code: #include<cstring> #include<algorithm> #include<cstdio> using namespace std; ...
- USACO 2012 March Silver Tractor /// 优先队列BFS oj21567
题目大意: 输入n,(x,y):n为阻挡的草堆数量,(x,y)为开始时拖拉机所在的位置 接下来n行每行一个坐标(a,b):为各个草堆的坐标 输出拖拉机要回到原点(0,0)需要移动的草堆数量 Sampl ...
随机推荐
- FM算法及FFM算法
转自:http://tech.meituan.com/deep-understanding-of-ffm-principles-and-practices.html http://blog.csdn. ...
- php.ini中extension默许的地址到底在哪里设置的
原文: http://www.myexception.cn/php/1436096.html ----------------------------------------------------- ...
- 编译iOS使用的.a库文件
首先是须要编译成.a的源文件 hello.h: #ifndef __INCLUDE_HELLO_H__ #define __INCLUDE_HELLO_H__ void hello(const cha ...
- protobuf 一个c++示例
http://wangjunle23.blog.163.com/blog/static/11783817120126155282640/ 1.在.proto文件中定义消息格式 2.使用prot ...
- Linux - Ubuntu中文输入法安装(Ubuntu 12.04)
Ubuntu中文输入法安装(Ubuntu 12.04) 本文地址:http://blog.csdn.net/caroline_wendy Ubuntu作为Linux常见的操作系统,是须要熟练使用的. ...
- Selenium系列之--04 常见元素操作总结
一.Selenium总共有八种定位方法 By.id() 通过id定位 By.name() 通过name 定位 By.xpath() 通过xpath定位 By.className() 通过clas ...
- wampserver64安装时出现计算机缺少MCVR110.DLL无法安装等
在安装wamp完成后运行出现上述问题,是因为wamp版本与DLL不对称.下面给出 wamp64位下载地址 http://www.onlinedown.net/soft/118187.htm vcred ...
- linux中用anaconda使用不同版本python
1.使用命令conda create --name python36 python=3.6 #你想使用哪个版本就下载哪个版本,--name后面跟的是该虚拟环境的名称 2.需要使用python3.6时 ...
- shell操作Hbase
status:查询集群的一些状态 hbase(main):002:0> status1 active master, 0 backup masters, 1 servers, 0 dead, 3 ...
- hadoop打jar包
编译: javac -classpath hadoop的路径下面/hadoop-0.20.0-core.jar -d .class文件存放的路径 XXXX.j ...