Sung's Placement Test

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner in  = new Scanner(System.in);
        int T = in.nextInt(); //the number of test cases
        for(int tc = 1; tc <= T; tc++) {
            int N = in.nextInt(); // the number of the students
            int Kmin = in.nextInt();
            int Kmax = in.nextInt();
            int[] scores = new int[1001];
            int Smin = 100; //the min score
            int Smax = 1;   //the max score
            for(int i = 0; i < N; i++) {
                scores[i] = in.nextInt();
                if(scores[i] > Smax) {
                    Smax = scores[i];
                }
                if(scores[i] < Smin) {
                    Smin = scores[i];
                }
            }
            int result = 1000;
            for(int T1 = Smin; T1 < Smax; T1++) {
                for(int T2 = Smax; T2 > Smin; T2--) {
                    int[] nums = new int[3]; //the numbers of Class A, B, C
                    boolean flag = true;
                    for(int j = 0; j < N; j++) {
                        if(scores[j] >= T2) {
                            nums[0]++;  //the num of Class A
                            if(nums[0] > Kmax) {
                                flag = false;
                                break;
                            }
                        }else if(scores[j] < T1) {
                            nums[2]++;  //the num of Class C
                            if(nums[2] > Kmax) {
                                flag = false;
                                break;
                            }
                        }else {
                            nums[1]++;
                            if(nums[1] > Kmax) {
                                flag = false;
                                break;
                            }
                        }
                    }
                    for(int x = 0; x < 3; x++) {
                        if(nums[x] < Kmin) {
                            flag = false;
                        }
                    }
                    if(flag) {
                        sort(nums);
                        int temp = nums[2] - nums[0];
                        if(temp < result) {
                            result = temp;
                        }
                    }
                }
            }
            if(result == 1000) {
                result = -1;
            }
            System.out.println("#"+tc+" "+result);
        }
    }
    
    public static void sort(int[] array) {
        int length = array.length;
        for(int i = 0; i < length; i++) {
            for(int j = i+1; j < length; j++) {
                if(array[i] > array[j]) {
                    int temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
        }
    }
}

 

  Placement rules:

  1) If> = T2 score, then the student is assigned to Class A;

  2) If the score> = T1 && <T2, then the student is assigned to Class B;

  3) If the score <T1, the students are assigned to the Class C.

  4) The minimum class size can not be less than Kmin (> = Kmin), can not exceed the maximum number Kmax (<= Kmax)

  Now, in T1, T2 is not given the circumstances, given to placement of the students' grades, in accordance with the requirements allocation rules to placement and obtained the largest number of number of classes with the least number of class differences, if there are multiple possible, the required minimum value, if not in accordance with the allocation rule, -1 is output.

  Input requirements:

  1) The first line, type T, expressed T test cases

  2) the second line, enter each test case, in which each case, the first line of the input three numbers, N represents the number (1-1000) students, Kmin represents the lower limit of the number of each class, each representative of Kmax the upper limit of the number of classes

  3) Enter the student's scores (1-100)

  Output requirements:

  Output (# + test_cast + "" + result)

 

  Ideas:

  Title, to define the placement of T1, T2 and unknown, so there may be more likely. We can identify students in the highest score and the lowest score, as a boundary, continue to look for possible T1, T2, and check the class size to meet the requirements, if met, compared with the current result, to find out more bonding value requirements of the subject. Find the highest and lowest points where the points, a very intuitive approach is to score (int [] scores) sort, then scores [0] is the lowest score, scores [N-1] is the highest score. Another approach is to first set up a special division with the lowest score, when students enter scores, continuously compared with the current highest score and the lowest divided into, and timely updates.

 

Guess you like

Origin www.cnblogs.com/WakingShaw/p/12636329.html