第一题
package ccf202104; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class p1 { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String[] temp = bf.readLine().split(" "); int n = Integer.parseInt(temp[0]); int m = Integer.parseInt(temp[1]); int L = Integer.parseInt(temp[2]); int[] h = new int[L]; int[][] a = new int[n][m]; for (int i = 0; i < n; i++) { temp = bf.readLine().split(" "); for (int j = 0; j < temp.length; j++) { a[i][j] = Integer.parseInt(temp[j]); h[a[i][j]]++; } } for (int i = 0; i < L; i++) { System.out.print(h[i] + " "); } } }
第二题(使用了二维前缀和,邻域不只是包括该元素周围的元素,也包括该元素自身)
package ccf202104; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class p2 { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String[] temp = bf.readLine().split(" "); int n = Integer.parseInt(temp[0]); int L = Integer.parseInt(temp[1]); int r = Integer.parseInt(temp[2]); int t = Integer.parseInt(temp[3]); int[][] a = new int[n + 1][n + 1]; int[][] sum = new int[n + 1][n + 1]; double[][][] nei; for (int i = 1; i <= n; i++) { temp = bf.readLine().split(" "); int index = 0; for (int j = 1; j <= n; j++) { a[i][j] = Integer.parseInt(temp[index]); index++; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j]; } } nei = getNei(sum, a, n, r); int count = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { double compare = nei[i][j][0] / nei[i][j][1]; if (compare <= t) { count++; } } } System.out.println(count); } public static double[][][] getNei(int[][] sum, int[][] a, int n, int r) { double[][][] nei = new double[n + 1][n + 1][2]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { int x1 = i - r; int y1 = j - r; int x2 = i + r; int y2 = j + r; if (x1 < 1) { x1 = 1; } if (y1 < 1) { y1 = 1; } if (x2 > n) { x2 = n; } if (y2 > n) { y2 = n; } nei[i][j][0] = sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1]; nei[i][j][1] = (x2 - (x1 - 1)) * (y2 - (y1 - 1)) ; } } return nei; } }