2019 autumn session of millet recruit document questions the first question _ cooking contest prize

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

1. Title Description

  Millet canteen cooking contest will be held once a year, assuming a total of participating chefs n bits (n <1000), did not disclose the score after the game, but standing on the podium in a row every chef cooks can see with their neighboring chef (left or right) in lower scores than their own (see points higher than their own people score) score. After the end of the game to the bonus in 1K units, each chef will be issued at least 1K bonus, in addition, if a chef found his prize no higher than the lower score than their chef's bonus will not satisfied, as the game organizers, millet canteen at least how many bonus paid to the need for all chefs satisfaction.
Input Description:
  Each set of data is divided into n + 1 positive integers single space, wherein the first number is the number of entries of the cook, and n is the number of points behind every cook (0-100)
Output Description:
  output number at least K bonuses

2. Problem-solving ideas

From left to right, watch the chefs at his score on the left side, smaller than their own, plus one on the basis of his
right to left, watch the chefs at his score on the right side, smaller than his own, max (own right + 1)

3. Code

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sr = new Scanner(System.in);
        String str = sr.nextLine();
        String[] arr = str.split("\\s+");
        int a[] = new int[arr.length-1];
        for(int i = 0;i<a.length;i++){
            a[i] = Integer.parseInt(arr[i+1]);
        }
        System.out.println(solution(a));

    }
    private static int solution(int[] arr) {
        int bonus[] = new int[arr.length];
        for(int i = 0;i<arr.length;i++){
            bonus[i] = 1;
        }
        //从左到右看,从第二个到最后一个数的更新
        //看自己左侧的厨师分数,比自己小的,在他的基础上加一
        for(int i = 1;i<=arr.length-1;i++){
            //如果右边的数大于左边的数,右边的数 = 左边的数 + 1
            if(arr[i]>arr[i-1]){
                bonus[i] = bonus[i-1] + 1;
            }
        }
        //从右到左看,从第1个到倒数第二个的更新,
        //看自己右侧的厨师分数,比自己小的,max(自己,右侧+1)
        for(int j = arr.length-2;j>=0;j--){
            if(arr[j]>arr[j+1]){
                bonus[j] =Math.max(bonus[j+1]+1,bonus[j]);
            }
        }
        //计算得的数
        int res = 0;
        for(int i = 0;i<bonus.length;i++){
            res +=bonus[i];
        }
        return res;
    }
}

Guess you like

Origin blog.csdn.net/qq_17556191/article/details/95225828