Untitled

 avatar
unknown
java
2 years ago
2.8 kB
6
Indexable
package z9;

import java.util.*;

class Z9Pierwsze {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[][] documents = new int[n][5];

        for (int i = 0; i < n; i++) {
            documents[i][0] = scanner.nextInt();
            documents[i][1] = scanner.nextInt();
            documents[i][2] = 0;
        }
        
        int juliaTime = 0;
        int winstonTime = 0;
        int currentDoc = 0;

        int time = 0;
        while (currentDoc != Integer.MIN_VALUE){
            if (time >= juliaTime){
                juliaTime = time;
                documents = checkUnlock(documents, 1, time, juliaTime);
                currentDoc = findMaxValueInColumn(documents, 0, 1);
                if (currentDoc != -1){
                    juliaTime += documents[currentDoc][0];
                    documents[currentDoc][2] = 1;
                    documents[currentDoc][3] = 1;
                }
            }

            if (time >= winstonTime){
                winstonTime = time;
                documents = checkUnlock(documents, 2, time, winstonTime);
                currentDoc = findMaxValueInColumn(documents, 1, 2);
                if (currentDoc != -1){
                    winstonTime += documents[currentDoc][1];
                    documents[currentDoc][2] = 2;
                    documents[currentDoc][4] = 1;
                }
            }
            time++;
            if (finishCheck(documents))
                break;
        }

        System.out.println(Math.max(winstonTime,juliaTime));
    }

    private static int findMaxValueInColumn(int[][] documents, int column, int JW) {
        int maxValue = Integer.MIN_VALUE;
        int row = -1;
        int doneByColumn = (JW == 1) ? 3 : 4;
        int lockFor = (JW == 1) ? 2 : 1;

        for (int i = 0; i < documents.length; i++) {
            if (documents[i][doneByColumn] == 1 || documents[i][2] == lockFor)
                continue;

            if (documents[i][column] > maxValue) {
                maxValue = documents[i][column];
                row = i;
            }
        }
        return row;
    }

    private static int[][] checkUnlock(int[][] documents, int JW, int time, int JWTime) {
        for (int i = 0; i < documents.length; i++) {
            if (documents[i][2] == JW && time >= documents[i][JW-1])
                documents[i][2] = 0;
        }

        return documents;
    }

    private static boolean finishCheck(int[][] documents){
        for (int i = 0; i < documents.length; i++) {
            if (documents[i][3] == 0 || documents[i][4] == 0)
                return false;
        }
        return true;
    }
}

Editor is loading...