[USACO13MAR]The Cow Run G/S 题解

题目传送门
附原题:

题目描述

Farmer John has forgotten to repair a hole in the fence on his farm, and his N cows (1 <= N <= 1,000) have escaped and gone on a rampage! Each minute a cow is outside the fence, she causes one dollar worth of damage. FJ must visit each cow to install a halter that will calm the cow and stop the damage.

Fortunately, the cows are positioned at distinct locations along a straight line on a road outside the farm. FJ knows the location P_i of each cow i (-500,000 <= P_i <= 500,000, P_i != 0) relative to the gate (position 0) where FJ starts.

FJ moves at one unit of distance per minute and can install a halter instantly. Please determine the order that FJ should visit the cows so he can minimize the total cost of the damage; you should compute the minimum total damage cost in this case.

农夫约翰的牧场围栏上出现了一个洞,有N(1 <= N <= 1,000)只牛从这个洞逃出了牧场。这些出逃的奶牛很狂躁,他们在外面到处搞破坏,每分钟每头牛都会给约翰带来1美元的损失。约翰必须用缰绳套住所有的牛,以停止他们搞破坏。

幸运的是,奶牛们都在牧场外一条笔直的公路上,牧场的大门恰好位于公里的0点处。约翰知道每头牛距离牧场大门的距离P_i(-500,000 <= P_i <= 500,000, P_i != 0)

约翰从农场大门出发,每分钟移动一个单位距离,每到一头牛所在的地点,约翰就会给它套上缰绳,套缰绳不花时间。按怎样的顺序去给牛套缰绳才能使约翰损失的费用最少?

输入格式
  • Line 1: The number of cows, N.

  • Lines 2…N+1: Line i+1 contains the integer P_i.

输出格式
  • Line 1: The minimum total cost of the damage.
输入输出样例
输入 #1
4 
-2 
-12 
3 
7 
输出 #1
50
说明/提示

Four cows placed in positions: -2, -12, 3, and 7.

The optimal visit order is -2, 3, 7, -12. FJ arrives at position -2 in 2 minutes for a total of 2 dollars in damage for that cow.

He then travels to position 3 (distance: 5) where the cumulative damage is 2 + 5 = 7 dollars for that cow.

He spends 4 more minutes to get to 7 at a cost of 7 + 4 = 11 dollars for that cow.

Finally, he spends 19 minutes to go to -12 with a cost of 11 + 19 = 30 dollars.

The total damage is 2 + 7 + 11 + 30 = 50 dollars.

题目解析

先看一眼题目, N ≤ 1000 N\leq 1000 N1000 O ( N 2 ) O\left(N^2\right) O(N2) 算法可以过。
我们可以发现,这道题目可以通过贪心或者搜索的方式来做,于是就可以小数据搜索大数据贪心
O ( 2 N ) O\left(2^N\right) O(2N)搜索肯定不行,保证TLE,那么我们考虑贪心。
贪心方案:向近一点的去,但是我们可以证明这是错的,我们构造一组数据如下:

4
-1 2 3 4

贪心是沿 − 1 → 2 → 3 → 4 -1→2→3→4 1234的道路走,耗费为 1 + 4 + 5 + 6 = 16 1+4+5+6=16 1+4+5+6=16
但是存在一条 2 → 3 → 4 → − 1 2→3→4→-1 2341的道路,耗费为 5 + 2 + 3 + 4 = 14 5+2+3+4=14 5+2+3+4=14
明显贪心是错误的。

我们发现,这道题貌似可以用贪心,也貌似可以用搜索,并且只要求我们输出答案并且不输出路径,那么这题的正解是什么呢?显然是DP。

如果有一道题,可以用贪心,也可以用搜索,这么这道题目一定是DP。——FLYing

FLYing:我没说过
我谔谔

DP式解析:
我们采用区间DP,首先想到用区间DP,但是我们发现仅仅靠两个维度是不能完成这道题目的,我们令 f i , j , k f_{i,j,k} fi,j,k来表示状态, i , j i,j i,j表示处理的区域的两个端点, k k k表示当前区间处理完成之后的位置, k = 0 k=0 k=0代表在左端, k = 1 k=1 k=1代表在右端。

代码版本1.0
f i , j , k f_{i,j,k} fi,j,k可以通过 f i + 1 , j , k   f i , j − 1 , k f_{i+1,j,k}\ f_{i,j-1,k} fi+1,j,k fi,j1,k得到
不难推出状态转移式:
f i , j , 0 = min ⁡ ( f i + 1 , j , 0 + ∣ a i + 1 − a i ∣ , f i + 1 , j , 1 + ∣ a j − a i ∣ ) f_{i,j,0}=\min\left( f_{i+1,j,0}+|a_{i+1}-a_i|,f_{i+1,j,1}+|a_j-a_i|\right) fi,j,0=min(f

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值