题解 | #合唱队#

合唱队

https://www.nowcoder.com/practice/6d9d69e3898f45169a441632b325c7b4

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int numOfStu = in.nextInt();
            int[][] numAndCount = new int[numOfStu][2];
            for (int i = 0; i < numOfStu; i++) {
                numAndCount[i][0] = in.nextInt();
            }

            for (int i = 0; i < numOfStu - 1; i++) {
                for(int j = 0; j < i; j++){
                    if(numAndCount[j][0] < numAndCount[i][0]){

                        numAndCount[i][1] = Math.max(numAndCount[j][1]+1, numAndCount[i][1]);
                    }
                }
            }

            int[] reverse = new int[numOfStu];
            for(int i = numOfStu -1; i > 0; i--){
                for(int j = i; j <= numOfStu - 1; j++){
                    if(numAndCount[j][0] < numAndCount[i][0]){
                        reverse[i] = Math.max(reverse[j] + 1, reverse[i]);
                    }
                }
            }
            int temp = 0;
            for(int i = 1 ; i < numOfStu -1; i++){
                if(temp < reverse[i] + numAndCount[i][1] + 1){
                    temp = reverse[i] + numAndCount[i][1] + 1;
                }
            }
            System.out.println(numOfStu - temp);
        }
    }
}

全部评论

相关推荐

哞客37422655...:这就是真实社会,没有花里胡哨的安慰,让你感受到阶级分明,不浪费彼此时间。虽然露骨但是唉
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务