The number of algorithm programming (Java) # prizes

Copyright: Author: xp_9512 Source: CSDN Copyright: This article is a blogger original article, reproduced, please attach Bowen link! Original: https://blog.csdn.net/qq_40981804/article/details/89530839

topic

There are n individuals to participate in the programming game, after the game everyone has a fraction
everyone now arranged in a circle (first individual and personal neighboring n) to collect their prizes, the requirements are met:

  • If a person than his neighbor who score high, then he won the prize should be superfluous neighboring people
  • Everyone needs to get at least a prize

Enter a description

Input Description:
The first line is an integer t, indicates the number of test samples of the
first line of each test sample is a positive integer n, the number of race (0 <n <100000)
second row is n integers a [i], each represents a fraction (0 <a [i] < 100000)
output description:
logarithmic minimum prize for each test sample, the output should be prepared to
example 1
input:
2
2
1 2
. 4
1 2 33
output:
. 3
. 8

Problem-solving ideas

First traverse from left to right, if the latter is greater than the preceding number, the number of prizes + 1, otherwise the prize number = 1, which ensure that everyone has at least one prize.

And then from right to left traversal, if the number is smaller than the latter in front of the number, this time, if the previous prize larger than the rear, is determined to continue, otherwise, the foregoing number of prizes have become Math.max (award [i], award [i +1] +1).

answer

public class test3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //测试样例个数
        int n = sc.nextInt();
        //存结果
        ArrayList<Integer> res = new ArrayList<Integer>();
        while (n-- != 0) {
            //人数
            int people = sc.nextInt();
            int[] score = new int[people+1];
            //保存分数
            for (int i = 0; i < people; i++) {
                score[i] = sc.nextInt();
            }
            score[people] = score[0];
            int res1 = countAward(score);
            res.add(res1);
        }
        for (Integer i :res)
            System.out.println(i);
    }
 
    private static int countAward(int[] score) {
        int res1 = 0;
        int[] award = new int[score.length];
        score[0] = 1;
        award[0] = 1;
        //先从左往右遍历,满足了每个人都至少有一个奖品
        for (int i = 1; i < score.length; i++) {
            if (score[i] > score[i - 1])
                award[i] = award[i - 1] + 1;
            else
                award[i] = 1;
        }
        //再从右往左遍历
        for (int i = score.length - 2; i > -1; i--) {
            if (score[i]> score[i+1]) {
                if (award[i] > award[i+1])
                    continue;
                else
                    award[i] = Math.max(award[i],award[i+1]+1);
            }
        }
        for (int i = 0; i < award.length-1; i++) {
            res1 += award[i];
        }
        return res1;
    }
}

Guess you like

Origin blog.csdn.net/qq_40981804/article/details/89530839