Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.10 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. import java.util.stream.IntStream;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         final Scanner scanner = new Scanner(System.in);
  10.  
  11.         final byte N = scanner.nextByte();
  12.         final byte M = scanner.nextByte();
  13.         final byte[] matchesTeam1 = new byte[N];
  14.         final byte[] matchesTeam2 = new byte[M];
  15.  
  16.         for (byte i = 0; i < N; i++) {
  17.             matchesTeam1[i] = scanner.nextByte();
  18.         }
  19.  
  20.         for (byte j = 0; j < M; j++) {
  21.             matchesTeam2[j] = scanner.nextByte();
  22.         }
  23.  
  24.         final Solver solver = new Solver(N, M, matchesTeam1, matchesTeam2);
  25.         solver.solve();
  26.  
  27.     }
  28.  
  29.  
  30.     static class Solver {
  31.  
  32.         private final byte N;
  33.         private final byte M;
  34.         private final byte[] matchesLeftTeam1;
  35.         private final byte[] matchesLeftTeam2;
  36.  
  37.         private final byte[] undefinedLeftTeam1;
  38.         private final byte[] undefinedLeftTeam2;
  39.  
  40.         private final DoesMatch[][] output;
  41.  
  42.         public Solver(byte N, byte M, byte[] matchesTeam1, byte[] matchesTeam2) {
  43.             this.N = N;
  44.             this.M = M;
  45.             this.matchesLeftTeam1 = matchesTeam1;
  46.             this.matchesLeftTeam2 = matchesTeam2;
  47.  
  48.             this.undefinedLeftTeam1 = new byte[N];
  49.             Arrays.fill(undefinedLeftTeam1, M);
  50.             this.undefinedLeftTeam2 = new byte[M];
  51.             Arrays.fill(undefinedLeftTeam2, N);
  52.  
  53.             output = new DoesMatch[N][M];
  54.             for (DoesMatch[] row : output) {
  55.                 Arrays.fill(row, DoesMatch.UNDEFINED);
  56.             }
  57.         }
  58.  
  59.         public void solve() {
  60.             try {
  61.                 if (IntStream.range(0, matchesLeftTeam1.length).map(i -> matchesLeftTeam1[i]).sum() != IntStream.range(0, matchesLeftTeam2.length).map(j -> matchesLeftTeam2[j]).sum()) {
  62.                     throw new IllegalStateException();
  63.                 }
  64.  
  65.                 for (byte i = 0; i < N; i++) {
  66.                     processTeam1(i);
  67.                 }
  68.  
  69.                 for (byte j = 0; j < M; j++) {
  70.                     processTeam2(j);
  71.                 }
  72.  
  73.                 for (byte i = 0; i < N; i++) {
  74.                     for (byte j = 0; j < M; j++) {
  75.                         if (output[i][j] == DoesMatch.UNDEFINED) {
  76.                             setMatch(i, j, false);
  77.                         }
  78.                     }
  79.                 }
  80.  
  81.                 for (byte i = 0; i < N; i++) {
  82.                     if (matchesLeftTeam1[i] != 0) {
  83.                         throw new IllegalStateException();
  84.                     }
  85.                 }
  86.                 for (byte j = 0; j < M; j++) {
  87.                     if (matchesLeftTeam2[j] != 0) {
  88.                         throw new IllegalStateException();
  89.                     }
  90.                 }
  91.                 printCurrentStatus();
  92.             } catch (IllegalStateException e) {
  93.                 System.out.println("-1");
  94.             }
  95.         }
  96.  
  97.         public void printCurrentStatus() {
  98.             for (byte i = 0; i < N; i++) {
  99.                 for (byte j = 0; j < M; j++) {
  100.                     System.out.print(output[i][j].toString());
  101.                 }
  102.                 if (i != N - 1) {
  103.                     System.out.println();
  104.                 }
  105.             }
  106.         }
  107.  
  108.         public void processTeam1(byte i) {
  109.             if (matchesLeftTeam1[i] == 0) {
  110.                 for (byte j1 = 0; j1 < M; j1++) {
  111.                     if (output[i][j1] == DoesMatch.UNDEFINED) {
  112.                         setMatch(i, j1, false);
  113.                     }
  114.                 }
  115.             } else if (undefinedLeftTeam1[i] == matchesLeftTeam1[i]) {
  116.                 for (byte j1 = 0; j1 < M; j1++) {
  117.                     if (output[i][j1] == DoesMatch.UNDEFINED) {
  118.                         setMatch(i, j1, true);
  119.                     }
  120.                 }
  121.             }
  122.         }
  123.  
  124.         public void processTeam2(byte j) {
  125.             if (matchesLeftTeam2[j] == 0) {
  126.                 for (byte i1 = 0; i1 < N; i1++) {
  127.                     if (output[i1][j] == DoesMatch.UNDEFINED) {
  128.                         setMatch(i1, j, false);
  129.                     }
  130.                 }
  131.             } else if (undefinedLeftTeam2[j] == matchesLeftTeam2[j]) {
  132.                 for (byte i1 = 0; i1 < N; i1++) {
  133.                     if (output[i1][j] == DoesMatch.UNDEFINED) {
  134.                         setMatch(i1, j, true);
  135.                     }
  136.                 }
  137.             }
  138.         }
  139.  
  140.         public void setMatch(byte i, byte j, boolean doesMatch) {
  141.             if (doesMatch) {
  142.                 switch (output[i][j]) {
  143.                     case YES:
  144.                         return;
  145.                     case NO:
  146.                         throw new IllegalStateException("");
  147.                     case UNDEFINED:
  148.                         output[i][j] = DoesMatch.YES;
  149.                         undefinedLeftTeam1[i]--;
  150.                         matchesLeftTeam1[i]--;
  151.                         undefinedLeftTeam2[j]--;
  152.                         matchesLeftTeam2[j]--;
  153.                         processTeam1(i);
  154.                         processTeam2(j);
  155.                 }
  156.             } else {
  157.                 switch (output[i][j]) {
  158.                     case YES:
  159.                         throw new IllegalStateException("");
  160.                     case NO:
  161.                         return;
  162.                     case UNDEFINED:
  163.                         output[i][j] = DoesMatch.NO;
  164.                         undefinedLeftTeam1[i]--;
  165.                         undefinedLeftTeam2[j]--;
  166.                         processTeam1(i);
  167.                         processTeam2(j);
  168.                 }
  169.             }
  170.         }
  171.  
  172.         enum DoesMatch {
  173.             UNDEFINED("UNDEFINED"),
  174.             YES("1"),
  175.             NO("0");
  176.  
  177.             private final String stringVal;
  178.  
  179.             DoesMatch(String stringVal) {
  180.                 this.stringVal = stringVal;
  181.             }
  182.  
  183.             @Override
  184.             public String toString() {
  185.                 return stringVal;
  186.             }
  187.  
  188.         }
  189.  
  190.     }
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement