|
| 1 | +package Simulation.swea2115; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +public class Solution { |
| 7 | + |
| 8 | + static int T, N, M, C; |
| 9 | + static int[][] map; |
| 10 | + static int[] select; |
| 11 | + |
| 12 | + public static void main(String[] args) throws Exception { |
| 13 | + System.setIn(new FileInputStream("src/Simulation/swea2115/sample_input.txt")); |
| 14 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 15 | + StringBuilder sb = new StringBuilder(); |
| 16 | + |
| 17 | + T = Integer.parseInt(br.readLine()); |
| 18 | + for (int t = 1; t <= T; t++) { |
| 19 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 20 | + N = Integer.parseInt(st.nextToken()); |
| 21 | + M = Integer.parseInt(st.nextToken()); |
| 22 | + C = Integer.parseInt(st.nextToken()); |
| 23 | + |
| 24 | + map = new int[N][N]; |
| 25 | + for (int i = 0; i < N; i++) { |
| 26 | + st = new StringTokenizer(br.readLine()); |
| 27 | + for (int j = 0; j < N; j++) { |
| 28 | + map[i][j] = Integer.parseInt(st.nextToken()); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + int rcnt = N-M+1; |
| 33 | + select = new int[N*rcnt]; |
| 34 | + for (int i = 0, cnt = 0; i < N; i++) { |
| 35 | + for (int j = 0; j < rcnt; j++) { |
| 36 | + select[cnt++] = collect(i, j); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + int ans = 0; |
| 41 | + for (int i = 0, size = N*rcnt, r = rcnt; i < size; i++) { |
| 42 | + int j = i+M; |
| 43 | + for (; j < r; j++) ans = Math.max(ans, select[i] + select[j]); |
| 44 | + for (j = r; j < size; j++) ans = Math.max(ans, select[i] + select[j]); |
| 45 | + if (i == r-1) r += rcnt; |
| 46 | + } |
| 47 | + |
| 48 | + sb.append("#").append(t).append(" ").append(ans).append("\n"); |
| 49 | + } |
| 50 | + |
| 51 | + System.out.println(sb.toString()); |
| 52 | + } |
| 53 | + |
| 54 | + static int collect(int i, int j) { |
| 55 | + int res = 0; |
| 56 | + int[] sub = Arrays.copyOfRange(map[i], j, j+M); |
| 57 | + Arrays.sort(sub); |
| 58 | + for (int flag = 1; flag < 1<<M; flag++) { |
| 59 | + int amt = 0; |
| 60 | + for (int k = M-1, c = 0; k >= 0; k--) { |
| 61 | + if ((flag & 1<<k) != 0) { |
| 62 | + c += sub[k]; |
| 63 | + if (c > C) break; |
| 64 | + amt += sub[k]*sub[k]; |
| 65 | + } |
| 66 | + } |
| 67 | + res = Math.max(res, amt); |
| 68 | + } |
| 69 | + return res; |
| 70 | + } |
| 71 | +} |
0 commit comments