Untitled

mail@pastecode.io avatar
unknown
java
a year ago
4.3 kB
1
Indexable
Never


import java.util.*;

public class Main{

    public static void main(String[] args){
                Scanner scanner = new Scanner(System.in);

        int m = scanner.nextInt();
        int n = scanner.nextInt();

        ArrayList<Integer> reports = new ArrayList<>();

        for (int i = 0; i < n; i++) {
            Integer r = scanner.nextInt();
            reports.add(r);
        }

        reports.sort(Collections.reverseOrder());

        int[] results = new int[m];
        while (reports.size() > 0){
            for (int i = 0; i < m; i++){
                if (reports.size() > 0){
                    results[i] += reports.get(0);
                    reports.remove(0);
                }
            }
            Arrays.sort(results);
        }

        System.out.println(results[m-1]);
    }

}


////// 5.2

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int m = sc.nextInt();
        int n = sc.nextInt();

        int[] reports = new int[n];
        int totalTime = 0;
        int longestReport = Integer.MIN_VALUE;
        int reporta = 0;
        for (int i = 0; i < n; i++) {
            reporta = sc.nextInt();
            reports[i] = reporta;
            totalTime += reports[i];

            if (reporta > longestReport){
                longestReport = reporta;
            }
        }

        int avg = totalTime / m;
        int avgTime = Math.max(avg, longestReport);
        int[] workingTimes = new int[m];
        int[][] reportsRange = new int[m][n];
        int[][] reportsDoneBy = new int[m][n];
        int[][] reportsSheet = new int [200][4];

        int currTime = 0;
        int currWorker = 0;
        int currReport = 0;
        int row = 0;

        for (int i = 0; i < n; i++){
            int startTime = currTime;
            int endTime = currTime + reports[currReport];

            if (endTime <= avgTime){
                reportsSheet[row][0] = currWorker;
                reportsSheet[row][1] = startTime;
                reportsSheet[row][2] = endTime;
                reportsSheet[row][3] = currReport + 1;
                row++;
                currReport++;
                currTime = endTime;
            } else {
                if (startTime == avgTime){
                    currWorker++;
                    currTime = 0;
                    startTime = currTime;
                    endTime = currTime + reports[currReport];

                    reportsSheet[row][0] = currWorker;
                    reportsSheet[row][1] = startTime;
                    reportsSheet[row][2] = endTime;
                    reportsSheet[row][3] = currReport + 1;
                    row++;
                    currReport++;
                    currTime = endTime;
                } else {
                    endTime = avgTime;
                    reportsSheet[row][0] = currWorker;
                    reportsSheet[row][1] = startTime;
                    reportsSheet[row][2] = endTime;
                    reportsSheet[row][3] = currReport + 1;
                    row++;
                    currTime = endTime;
                    i--;
                    reports[currReport] = reports[currReport] - (avgTime - startTime);
                }

            }

        }

        // Output the results
        System.out.println(avgTime);

        int counter = 0;
        int worker = 0;
        for (int i = 0; i < m; i++){
            System.out.print(worker + 1 + ": ");
            while (reportsSheet[counter][0] == worker){
                int startTime = reportsSheet[counter][1];
                int endTime = reportsSheet[counter][2];
                int report = reportsSheet[counter][3];
                System.out.print((report) + "[" + startTime + "," + endTime + "] ");
                counter++;
            }
            worker++;
            System.out.println();
        }

//        for (int i = 0; i < row; i++){
//            System.out.print(i + 1 + ": ");
//            for (int j = 0; j < 3; j++){
//                int startTime = reportsSheet[i][1];
//                int endTime = reportsSheet[i][2];
//                System.out.print((j+1) + "[" + startTime + "," + endTime + "] ");
//            }
//            System.out.println();
//        }
    }
}