Cod sursa(job #2421788)

Utilizator RazvanIIancu Razvan RazvanI Data 16 mai 2019 09:45:12
Problema Taramul Nicaieri Scor 5
Compilator java Status done
Runda Arhiva de probleme Marime 2.21 kb
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    static int n ;
    static int[] ston;
    static int[] ntot;
    public static void readInput(){
        try {
            Scanner sc = new Scanner(new BufferedReader(new FileReader("harta.in")));
            n = sc.nextInt();
            ston = new int[n+1];
            ntot = new int[n+1];
            for ( int i = 1 ; i <= n ;i++){
                ston[i] = sc.nextInt();
                ntot[i] = sc.nextInt();
            }
            sc.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
    static ArrayList<Integer> edges = new ArrayList<>();
    private static void writeOutput(int result) {
        try {
            PrintWriter pw = new PrintWriter(new File("harta.out"));
            pw.printf("%d\n", result);
            if (result != -1) {
                for (int i = 0; i < edges.size(); i += 2) {
                    pw.printf("%d %d\n", edges.get(i), edges.get(i + 1));
                }
            }
            pw.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) {
        readInput();
        ArrayList<Integer> alreadyin[] = new ArrayList[1000];
        for ( int i = 0 ; i <= n ; i++){
            alreadyin[i] = new ArrayList<>();
        }
        for (int i = 1; i <= n; i++) {
            while (ston[i] != 0) {
                int max = Integer.MIN_VALUE;
                int maxindex = -1;
                for (int j = 1; j <= n; j++) {
                    if ( i!=j && !alreadyin[i].contains(j) && max < ntot[j]) {
                        max = ntot[j];
                        maxindex = j;
                    }
                }
                if (maxindex != -1) {
                    ntot[maxindex]--;
                    ston[i]--;
                    alreadyin[i].add(maxindex);
                    System.out.println(i + " " + maxindex);
                    edges.add(i);
                    edges.add(maxindex);
//                    max = Integer.MIN_VALUE;
//                    break;
                }
            }
        }
        writeOutput(edges.size()/2);

    }

}