LintCode(M)最小调整代价(待续)

最初有bug的代码:

class Solution {
public:
    /**
     * @param A: An integer array.
     * @param target: An integer.
     */
   int MinAdjustmentCost(vector<int> A, int target) {


    // write your code here
    int  sum = 0, i;
    float p, q,w,ping = 0;
    w = target;
    //target = (float)target;
    for (i = 0; i//  cout <
    p = floor(ping + w / 2);
    q = (int)(ping - w / 2);

//  cout << p << endl << q<
    for (i = 0; iif (A[i]>p)
            sum += A[i] - p;
        if(A[i]return sum;

}
};

其中注意点:
1.
target和ping是int型,要转换成float型,不然p和q值一样。
2.
注意q不要用floor函数。
这是我最开始用的方案,用的是平均数,之后改成中位数,但是结果还是不行。
这个思路不行在于它把所有的数都局限在一个大范围里,但是却忽略了像循序渐进的等差数列,就像楼梯,不断递增这种情况。
决定转换思路

你可能感兴趣的:(LintCode)