题目传送门
附原题:
题目描述
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 N≤1000, 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 −1→2→3→4的道路走,耗费为 1 + 4 + 5 + 6 = 16 1+4+5+6=16 1+4+5+6=16
但是存在一条 2 → 3 → 4 → − 1 2→3→4→-1 2→3→4→−1的道路,耗费为 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,j−1,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