// 第二题, 思路是最长*子序列 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()){ int T = sc.nextInt(); int[] res = new int[T]; for (int i = 0; i < T; i++){ int N = sc.nextInt(); int[] arr = new int[N]; for (int j = 0; j < N; j++){ arr[j] = sc.nextInt(); } res[i] = dp(arr); } for (int i = 0; i < res.length; i++){ if (i >= res.length - 1){ System.out.print(res[i]); break; } System.out.println(res[i]); } } } public static int dp(int[] arr){ int[] Up = new int [arr.length]; Up[0] = 1; int[] Down = new int [arr.length]; Down[0] = 1; int res = 0; for (int i = 1; i < arr.length; i++){ for (int j = i - 1; j >= 0; j--){ // 升 if (arr[j] <= arr[i]) { Up[i] = Math.max(Up[j] + 1, Up[i]); } else { Down[i] = Math.max(Down[j] + 1, Down[i]); } } res = Math.max(res, Math.max(Up[i] , Down[i])); } return arr.length - res; } }
// 第一题, 10% import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()){ int M = sc.nextInt(); int N = sc.nextInt(); if (M <= 0 || N <= 0){ continue; } int[][] inputs = new int[M][N]; int[] sum = new int[N]; for (int i = 0; i < M; i++){ for (int j = 0; j < N; j++){ inputs[i][j] = sc.nextInt(); sum[j] += inputs[i][j]; } } // mean int count = 0; double[] mean = new double[N]; for (int i = 0; i < N; i++){ mean[i] = sum[i] * 1.0 / M; } // i 每个学生 for (int i = 0; i < M; i++){ // j 每门成绩 for (int j = 0; j < N; j++){ if (mean[j] <= inputs[i][j]){ count++; break; } } } System.out.println(count); } } }
全部评论
(5) 回帖