递增队列、递减队列、转折队列

///代码还存在问题,稍后想一下/

题目描述:

* 小C在做一种特殊的服务器负载测试,对于一个请求队列中的请求,

* 每一个请求都有一个负荷值,为了保证服务器稳定,请求队列

* 中的请求负荷必须按照先递增后递减的规律(仅递增,仅递减也可以),

*比如[ 1,2,8,4,3 ],[ 1,3,5 ]和[ 10 ]这些是满足规律的,

* 还有一些不满足的,比如[ 1,2,2,1 ],[ 2,1,2 ]和[ 10,10 ]。

* 现在给你一个请求队列,你可以对请求的负荷值进行增加,

* 要求你调整队列中请求的负荷值,使数组满足条件。最后输出使队列满足条件最小的增加总和。

*  输入 5 2 5 3 2 4

* 输出 6

 

代码:

import java.util.Scanner;

public class test13 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextLong();
        }
        System.out.println(sortArr(n, arr));
    }


    public static long sortArr(int n, long[] arr) {
        long res = 0;
        long[] newArr = new long[n];
        long[] newArrInc = new long[n];
        long[] newArrDec = new long[n];
        System.arraycopy(arr, 0, newArrInc, 0, n);
        System.arraycopy(arr, 0, newArrDec, 0, n);
        long sum = arr[0];
        long sumInc = newArrInc[0];
        long sumDec = newArrDec[n - 1];
        for (int i = 1; i < n; i++) {
//            构造一个递增数列
            if (newArrInc[i] <= newArrInc[i - 1]) {
                newArrInc[i] = newArrInc[i - 1] + 1;
                sumInc += newArrInc[i];
            }
//            构造一个递减数列
            if (newArrDec[n - 1 - i] <= newArrDec[n - i]) {
                newArrDec[n - 1 - i] = newArrDec[n - i] + 1;
                sumDec += newArrDec[n - 1 - i];
            }
            sum += arr[i];
        }
//        判断是否有转折点
        int pointa = 0;
        int pointb = 0;
        int pointc = 0;
        for (int i = 0; i < n; i++) {
            if (newArrInc[i] == newArrDec[i]) {
                pointa = i;
                break;
            }
        }
        for (int i = 0; i < n - 1; i++) {
            if (newArrInc[i] == newArrDec[i + 1]) {
                pointb = i;
                break;
            }
            if (newArrInc[i + 1] == newArrDec[i]) {
                pointc = i;
                break;
            }
        }
        long inc = sumInc - sum;
        long dec = sumDec - sum;
//        拼接
//        如有转折点 point != 0
        if (pointa != 0) {
            System.arraycopy(newArrInc, 0, newArr, 0, pointa);
            System.arraycopy(newArrDec, pointa + 1, newArr, pointa + 1, n - pointa - 1);
            for (int i = 0; i < n; i++) {
                res += newArr[i];
                return res-sum;
            }
        }
        long[] newArr1 = new long[n];
        long[] newArr2 = new long[n];
        long res1 = 0;
        long res2 = 0;
        if (pointb != 0) {
            System.arraycopy(newArrInc, 0, newArr1, 0, pointb);
            System.arraycopy(newArrDec, pointb + 1, newArr1, pointb + 1, n - pointb - 1);

            System.arraycopy(newArrInc, 0, newArr2, 0, pointb - 1);
            System.arraycopy(newArrDec, pointb, newArr2, pointb, n - pointb);
            for (int i = 0; i < n; i++) {
                res1 += newArr1[i];
                res2 += newArr2[i];
            }
            return Math.min(res1-sum, res2-sum);
        }
        if (pointc != 0) {
            System.arraycopy(newArrInc, 0, newArr1, 0, pointc);
            System.arraycopy(newArrDec, pointc + 1, newArr1, pointc + 1, n - pointc - 1);

            System.arraycopy(newArrInc, 0, newArr2, 0, pointc - 1);
            System.arraycopy(newArrDec, pointc, newArr2, pointc, n - pointc);
            for (int i = 0; i < n; i++) {
                res1 += newArr1[i];
                res2 += newArr2[i];
            }
            return Math.min(res1-sum, res2-sum);
        }

        //如果没有转折点
        res = Math.min(inc, dec);
        return res;

    }
}

你可能感兴趣的:(笔试题)