Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- import java.util.stream.IntStream;
- public class Main {
- public static void main(String[] args) {
- final Scanner scanner = new Scanner(System.in);
- final byte N = scanner.nextByte();
- final byte M = scanner.nextByte();
- final byte[] matchesTeam1 = new byte[N];
- final byte[] matchesTeam2 = new byte[M];
- for (byte i = 0; i < N; i++) {
- matchesTeam1[i] = scanner.nextByte();
- }
- for (byte j = 0; j < M; j++) {
- matchesTeam2[j] = scanner.nextByte();
- }
- final Solver solver = new Solver(N, M, matchesTeam1, matchesTeam2);
- solver.solve();
- }
- static class Solver {
- private final byte N;
- private final byte M;
- private final byte[] matchesLeftTeam1;
- private final byte[] matchesLeftTeam2;
- private final byte[] undefinedLeftTeam1;
- private final byte[] undefinedLeftTeam2;
- private final DoesMatch[][] output;
- public Solver(byte N, byte M, byte[] matchesTeam1, byte[] matchesTeam2) {
- this.N = N;
- this.M = M;
- this.matchesLeftTeam1 = matchesTeam1;
- this.matchesLeftTeam2 = matchesTeam2;
- this.undefinedLeftTeam1 = new byte[N];
- Arrays.fill(undefinedLeftTeam1, M);
- this.undefinedLeftTeam2 = new byte[M];
- Arrays.fill(undefinedLeftTeam2, N);
- output = new DoesMatch[N][M];
- for (DoesMatch[] row : output) {
- Arrays.fill(row, DoesMatch.UNDEFINED);
- }
- }
- public void solve() {
- try {
- if (IntStream.range(0, matchesLeftTeam1.length).map(i -> matchesLeftTeam1[i]).sum() != IntStream.range(0, matchesLeftTeam2.length).map(j -> matchesLeftTeam2[j]).sum()) {
- throw new IllegalStateException();
- }
- for (byte i = 0; i < N; i++) {
- processTeam1(i);
- }
- for (byte j = 0; j < M; j++) {
- processTeam2(j);
- }
- for (byte i = 0; i < N; i++) {
- for (byte j = 0; j < M; j++) {
- if (output[i][j] == DoesMatch.UNDEFINED) {
- setMatch(i, j, false);
- }
- }
- }
- for (byte i = 0; i < N; i++) {
- if (matchesLeftTeam1[i] != 0) {
- throw new IllegalStateException();
- }
- }
- for (byte j = 0; j < M; j++) {
- if (matchesLeftTeam2[j] != 0) {
- throw new IllegalStateException();
- }
- }
- printCurrentStatus();
- } catch (IllegalStateException e) {
- System.out.println("-1");
- }
- }
- public void printCurrentStatus() {
- for (byte i = 0; i < N; i++) {
- for (byte j = 0; j < M; j++) {
- System.out.print(output[i][j].toString());
- }
- if (i != N - 1) {
- System.out.println();
- }
- }
- }
- public void processTeam1(byte i) {
- if (matchesLeftTeam1[i] == 0) {
- for (byte j1 = 0; j1 < M; j1++) {
- if (output[i][j1] == DoesMatch.UNDEFINED) {
- setMatch(i, j1, false);
- }
- }
- } else if (undefinedLeftTeam1[i] == matchesLeftTeam1[i]) {
- for (byte j1 = 0; j1 < M; j1++) {
- if (output[i][j1] == DoesMatch.UNDEFINED) {
- setMatch(i, j1, true);
- }
- }
- }
- }
- public void processTeam2(byte j) {
- if (matchesLeftTeam2[j] == 0) {
- for (byte i1 = 0; i1 < N; i1++) {
- if (output[i1][j] == DoesMatch.UNDEFINED) {
- setMatch(i1, j, false);
- }
- }
- } else if (undefinedLeftTeam2[j] == matchesLeftTeam2[j]) {
- for (byte i1 = 0; i1 < N; i1++) {
- if (output[i1][j] == DoesMatch.UNDEFINED) {
- setMatch(i1, j, true);
- }
- }
- }
- }
- public void setMatch(byte i, byte j, boolean doesMatch) {
- if (doesMatch) {
- switch (output[i][j]) {
- case YES:
- return;
- case NO:
- throw new IllegalStateException("");
- case UNDEFINED:
- output[i][j] = DoesMatch.YES;
- undefinedLeftTeam1[i]--;
- matchesLeftTeam1[i]--;
- undefinedLeftTeam2[j]--;
- matchesLeftTeam2[j]--;
- processTeam1(i);
- processTeam2(j);
- }
- } else {
- switch (output[i][j]) {
- case YES:
- throw new IllegalStateException("");
- case NO:
- return;
- case UNDEFINED:
- output[i][j] = DoesMatch.NO;
- undefinedLeftTeam1[i]--;
- undefinedLeftTeam2[j]--;
- processTeam1(i);
- processTeam2(j);
- }
- }
- }
- enum DoesMatch {
- UNDEFINED("UNDEFINED"),
- YES("1"),
- NO("0");
- private final String stringVal;
- DoesMatch(String stringVal) {
- this.stringVal = stringVal;
- }
- @Override
- public String toString() {
- return stringVal;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement