Pagini recente » Cod sursa (job #2554115) | Cod sursa (job #3276264) | Cod sursa (job #2505911) | Cod sursa (job #1985209) | Cod sursa (job #2421788)
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);
}
}