传送门: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. SQLAlchemy的group_by和order_by的区别

    1.官网解释: group_by(*criterion) apply one or more GROUP BY criterion to the query and return the newly ...

  2. Android 自己定义UI文章汇总

    <Android ListView分类/分组效果 - 第一种实现方式> <Android ListView分类/分组效果 - 另外一种实现方式> <Android Lis ...

  3. Linux中查看文件或者文件夹大小

    df -l 查看磁盘空间大小命令 df -hl  查看磁盘剩余空间 df -h  查看每个根路径的分区大小 du -sh  当前文件夹下所有文件大小(包括子文件大小 du -sm  [文件夹] 返回该 ...

  4. [Node.js] Write or Append to a File in Node.js with fs.writeFile and fs.writeFileSync

    In node.js, you can require fs, and then call fs.writeFile with the filename, and data to write to t ...

  5. Codeforces div.2 B. The Child and Set

    题目例如以下: B. The Child and Set time limit per test 1 second memory limit per test 256 megabytes input ...

  6. 初识mina框架

    step1:创建java工程,导入jar包 step2:在src下配置log4j.properties文件 log4j.rootLogger=DEBUG,MINA,file log4j.appende ...

  7. 基于 Vue.js 之 iView UI 框架非工程化实践记要 使用 Newtonsoft.Json 操作 JSON 字符串 基于.net core实现项目自动编译、并生成nuget包 webpack + vue 在dev和production模式下的小小区别 这样入门asp.net core 之 静态文件 这样入门asp.net core,如何

    基于 Vue.js 之 iView UI 框架非工程化实践记要   像我们平日里做惯了 Java 或者 .NET 这种后端程序员,对于前端的认识还常常停留在 jQuery 时代,包括其插件在需要时就引 ...

  8. Make 编译脚本上手

    考察下面的示例代码: main.c #include <stdio.h> int main(){ printf("hello world!"); return 0; } ...

  9. Linux Chromium安装Adobe Flash Player

    首先,下载: install_flash_player_11_linux.i386.tar.gz 解压文件: tar -xvf install_flash_player_11_linux.i386.t ...

  10. 10 逻辑完善以及bug修复

    进行到这里,我们应用开发已经接近尾声,我这里基本就是应用开发的记录过程,讲解的东西很少,有问题可以在评论区讨论呦.下面进入最后调整的阶段. 预览我们的应用,会发现首页的职位列表,也会显示收藏的星星图标 ...