Untitled
import java.util.Scanner; /* As the name of the class should be Solution, using Solution.java as the filename is recommended. In any case, you can execute your program by running 'java Solution' command. */ class Solution { static int t,n,l1,l2,l3,p1,p2,p3, max, min, re,maxElm; static int[][] time; static int[][] ads; static int[] visited; static int[][] vitri; public static void main(String[] args) { Scanner sc = new Scanner(System.in); t = sc.nextInt(); for (int i = 0; i < t; i++) { maxElm = 0; re = 0; max = 0; min = 50; n = sc.nextInt(); ads = new int[2][3]; ads[0][0] = sc.nextInt(); ads[0][1] = sc.nextInt(); ads[0][2] = sc.nextInt(); ads[1][0] = sc.nextInt(); ads[1][1] = sc.nextInt(); ads[1][2] = sc.nextInt(); max = ads[0][0] + ads[0][1] + ads[0][2]; if(maxElm < ads[0][0]) { maxElm = ads[0][0]; } if(maxElm < ads[0][1]) { maxElm = ads[0][1]; } if(maxElm < ads[0][2]) { maxElm = ads[0][2]; } time = new int[2][n]; for (int j = 0; j < n; j++) { time[0][j] = sc.nextInt(); time[1][j] = sc.nextInt(); int tem = time[0][j] + time[1][j]; if(tem > max){ max = tem; } if(min > time[0][j]){ min = time[0][j]; } } max +=maxElm; visited = new int[max]; vitri = new int[3][3]; //System.out.println(check(10,2)); // visited(2,2,1); // System.out.println(check(1, 1)); // for (int j = 0; j < max; j++) { // System.out.print(j +":" + visited[j] + " "); // } // System.out.println(); fun(0); System.out.println("Case #" + (i+1)); //System.out.println(max); System.out.println(re); } } static void fun(int ad){ if(ad == 3){ // for (int j = 0; j < max; j++) { // System.out.print(j +":" + visited[j] + " "); // } // System.out.println(); int score = score(); if( score > re){ re = score; } return; } int len = ads[0][ad]; int point = ads[1][ad]; for (int j = 0; j < max; j++) { if(check(j, len)){ visited(j,len,point); vitri[0][ad] = j; vitri[1][ad] = j + len -1; vitri[2][ad] = point; fun(ad + 1); unvisited(j, len); } } } static boolean check(int i, int len){ if(i + len > max ) { return false; } for (int j = i; j <= i + len -1; j++) { if(visited[j] != 0) return false; } return true; } static void visited(int i, int len, int vi){ int top = i + len - 1 >= max - 1 ? max - 1: (i+len - 1); for (int j = i; j <= top; j++) { visited[j] = vi; } for (int j = 0; j < ads.length; j++) { } } static void unvisited(int i, int len){ int top = i + len - 1 >= max - 1 ? max - 1: (i+len - 1); for (int j = i; j <= top; j++) { visited[j] = 0; } } static int score() { int re = 0; int[] arr = new int[n]; for (int i = 0; i < 3; i++) { int a = vitri[0][i]; int b = vitri[1][i]; int point = vitri[2][i]; for (int j = 0; j < n; j++) { if(a >= time[0][j] - 1 && b <= time[1][j] + time[0][j] - 2) { if(point > arr[j]) { arr[j] = point; } } } } for (int i = 0; i < n; i++) { re += arr[i]; } return re; } }
Leave a Comment