Lintcode: Minimum Adjustment Cost

1 Given an integer array, adjust each integers so that the difference of every adjcent integers are not greater than a given number target.
2
3 If the array before adjustment is A, the array after adjustment is B, you should minimize the sum of |A[i]-B[i]|
4
5 Note
6 You can assume each number in the array is a positive integer and not greater than 100
7
8 Example
9 Given [1,4,2,3] and target=1, one of the solutions is [2,3,2,3], the adjustment cost is 2 and it‘s minimal. Return 2.

这道题要看出是背包问题,不容易,跟FB一面paint house很像,比那个难一点

定义res[i][j] 表示前 i个number with 最后一个number是j,这样的minimum adjusting cost

如果第i-1个数是j, 那么第i-2个数只能在[lowerRange, UpperRange]之间,lowerRange=Math.max(0, j-target), upperRange=Math.min(99, j+target),

这样的话,transfer function可以写成:

for (int p=lowerRange; p<= upperRange; p++) {

  res[i][j] = Math.min(res[i][j], res[i-1][p] + Math.abs(j-A.get(i-1)));

}

 1 public class Solution {
 2     /**
 3      * @param A: An integer array.
 4      * @param target: An integer.
 5      */
 6     public int MinAdjustmentCost(ArrayList<Integer> A, int target) {
 7         // write your code here
 8         int[][] res = new int[A.size()+1][100];
 9         for (int j=0; j<=99; j++) {
10             res[0][j] = 0;
11         }
12         for (int i=1; i<=A.size(); i++) {
13             for (int j=0; j<=99; j++) {
14                 res[i][j] = Integer.MAX_VALUE;
15                 int lowerRange = Math.max(0, j-target);
16                 int upperRange = Math.min(99, j+target);
17                 for (int p=lowerRange; p<=upperRange; p++) {
18                     res[i][j] = Math.min(res[i][j], res[i-1][p]+Math.abs(j-A.get(i-1)));
19                 }
20             }
21         }
22         int result = Integer.MAX_VALUE;
23         for (int j=0; j<=99; j++) {
24             result = Math.min(result, res[A.size()][j]);
25         }
26         return result;
27     }
28 }
时间: 2024-12-23 23:02:23

Lintcode: Minimum Adjustment Cost的相关文章

Minimum Adjustment Cost

Given an integer array, adjust each integers so that the difference of every adjcent integers are not greater than a given number target. If the array before adjustment is A, the array after adjustment is B, you should minimize the sum of |A[i]-B[i]|

HDU1385 Minimum Transport Cost 【Floyd】+【路径记录】

Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7496    Accepted Submission(s): 1918 Problem Description These are N cities in Spring country. Between each pair of cities

HDU1385 Minimum Transport Cost

Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9052    Accepted Submission(s): 2383 Problem Description These are N cities in Spring country. Between each pair of cities

HDU 1385 Minimum Transport Cost 最短路径题解

本题就是使用Floyd算法求所有路径的最短路径,并且需要保存路径,而且更进一步需要按照字典顺序输出结果. 还是有一定难度的. Floyd有一种很巧妙的记录数据的方法,大多都是使用这个方法记录数据的. 不过其实本题数据不是很大,一般太大的数据也无法使用Floyd,因为效率是O(N^3). 所以其实也可以使用一般的Floyd算法,然后增加个三维数组记录数据.下面就是这种做法,0ms过了. #include <stdio.h> #include <vector> using std::v

HDU 1385 Minimum Transport Cost(Floyd 最短路 打印路径)

HDU 1385 大意: 有N个城市,然后直接给出这些城市之间的邻接矩阵,矩阵中-1代表那两个城市无道路相连,其他值代表路径长度. 如果一辆汽车经过某个城市,必须要交一定的钱(可能是过路费). 现在要从a城到b城,花费为路径长度之和,再加上除起点与终点外所有城市的过路费之和. 求最小花费,如果有多条路经符合,则输出字典序最小的路径. 思路: Floyd求最短路,打印路径即可. 1 /*--------------------------------------------------------

zoj1456 Minimum Transport Cost

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1456 题目大意:有N个城市,两个城市之间要么有一条运输路线,要么没有.现在有一些货物需要从一个城市运往另一个城市.运输费用包含两部分:通过两个城市之间运输线路的费用,以及通过一个城市时的纳税(起点城市和目标城市除外).要求输出费用最小,并且路径字典需序最小的线路. 所谓字典序就是,如果有1-->2-->和1-->3两条路可选当输出1-->2-->3.

hdu Minimum Transport Cost(按字典序输出路径)

 摘自:Think In Java 从技术角度说,OOP(面向对象程序设计)只是涉及抽象的数据类型.继承以及多形性,但另一些问题也可能显得非常重要.本节将就这些问题进行探讨.最重要的问题之一是对象的创建及破坏方式.对象需要的数据位于哪儿,如何控制对象的"存在时间"呢?针对这个问题,解决的方案是各异其趣的.C++认为程序的执行效率是最重要的一个问题,所以它允许程序员作出选择.为获得最快的运行速度,存储以及存在时间可在编写程序时决定,只需将对象放置在堆栈(有时也叫作自动或定域变量)或者静态

hdu 1385 Minimum Transport Cost (Floyd + 字典序打印路径)

Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7822    Accepted Submission(s): 2028 Problem Description These are N cities in Spring country. Between each pair of cities

zoj 1456 Minimum Transport Cost (Floyd+路径记录)

Minimum Transport Cost Time Limit: 2 Seconds      Memory Limit: 65536 KB These are N cities in Spring country. Between each pair of cities there may be one transportation track or none. Now there is some cargo that should be delivered from one city t