結果

問題 No.1078 I love Matrix Construction
ユーザー ccppjsrbccppjsrb
提出日時 2020-06-12 23:14:48
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 5,039 bytes
コンパイル時間 11,314 ms
コンパイル使用メモリ 212,032 KB
実行使用メモリ 35,452 KB
最終ジャッジ日時 2023-09-06 11:19:06
合計ジャッジ時間 13,669 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,676 KB
testcase_01 AC 3 ms
5,676 KB
testcase_02 AC 12 ms
9,960 KB
testcase_03 AC 29 ms
19,360 KB
testcase_04 AC 36 ms
23,288 KB
testcase_05 WA -
testcase_06 AC 14 ms
9,904 KB
testcase_07 AC 7 ms
8,684 KB
testcase_08 AC 34 ms
20,128 KB
testcase_09 WA -
testcase_10 AC 79 ms
35,452 KB
testcase_11 AC 39 ms
23,644 KB
testcase_12 AC 64 ms
32,836 KB
testcase_13 AC 71 ms
33,340 KB
testcase_14 AC 44 ms
26,824 KB
testcase_15 AC 68 ms
33,156 KB
testcase_16 WA -
testcase_17 AC 3 ms
5,680 KB
testcase_18 AC 10 ms
9,432 KB
testcase_19 AC 20 ms
13,336 KB
testcase_20 AC 21 ms
13,440 KB
testcase_21 AC 3 ms
5,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	ss := make([]int, n)
	tt := make([]int, n)
	uu := make([]int, n)
	var pp [500][500][2][]pair
	for i := 0; i < n; i++ {
		ss[i] = getNextInt(scanner) - 1
	}
	for i := 0; i < n; i++ {
		tt[i] = getNextInt(scanner) - 1
	}
	for i := 0; i < n; i++ {
		uu[i] = getNextInt(scanner)
	}
	aa := makeGrid(n, n)
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			aa[ss[i]][j][0] = aa[ss[i]][j][0] | 1<<uint(uu[i])
			aa[j][tt[i]][1] = aa[j][tt[i]][1] | 1<<uint(uu[i])
			pp[ss[i]][j][0] = append(pp[ss[i]][j][0], newPair(j, tt[i], uu[i]))
			pp[j][tt[i]][1] = append(pp[j][tt[i]][1], newPair(ss[i], j, uu[i]))
		}
	}
	ans := make([][]int, n)
	for i := 0; i < n; i++ {
		ans[i] = make([]int, n)
		for j := 0; j < n; j++ {
			ans[i][j] = -1
		}
	}
	ll := [2]uint{0, 2}
	rr := [2]uint{0, 1}
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			for l := 0; l < 2; l++ {
				for r := 0; r < 2; r++ {
					if ll[l] == rr[r] {
						continue
					}
					if aa[i][j][0]>>ll[l]%2 == 1 && aa[i][j][1]>>rr[r]%2 == 1 {
						ans[i][j] = 1
					}
				}
			}
			for l := 0; l < 2; l++ {
				if aa[i][j][0]>>ll[l]%2 == 1 && aa[i][j][0]>>ll[1-l]%2 == 1 {
					ans[i][j] = 1
				}
			}
			for r := 0; r < 2; r++ {
				if aa[i][j][1]>>rr[r]%2 == 1 && aa[i][j][1]>>rr[1-r]%2 == 1 {
					ans[i][j] = 1
				}
			}
		}
	}
	ll = [2]uint{1, 3}
	rr = [2]uint{2, 3}
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			for l := 0; l < 2; l++ {
				for r := 0; r < 2; r++ {
					if ll[l] == rr[r] {
						continue
					}
					if aa[i][j][0]>>ll[l]%2 == 1 && aa[i][j][1]>>rr[r]%2 == 1 {
						if ans[i][j] == 1 {
							fmt.Fprintln(writer, -1)
							return
						}
						ans[i][j] = 0
					}
				}
			}
			for l := 0; l < 2; l++ {
				if aa[i][j][0]>>ll[l]%2 == 1 && aa[i][j][0]>>ll[1-l]%2 == 1 {
					if ans[i][j] == 1 {
						fmt.Fprintln(writer, -1)
						return
					}
					ans[i][j] = 0
				}
			}
			for r := 0; r < 2; r++ {
				if aa[i][j][1]>>rr[r]%2 == 1 && aa[i][j][1]>>rr[1-r]%2 == 1 {
					if ans[i][j] == 1 {
						fmt.Fprintln(writer, -1)
						return
					}
					ans[i][j] = 0
				}
			}
		}
	}

	updated := true
	for updated {
		updated = false
		for i := 0; i < n; i++ {
			for j := 0; j < n; j++ {
				if ans[i][j] == 1 {
					for _, p := range pp[i][j][1] {
						o := ans[p.y][p.x]
						if p.u == 3 {
							ans[p.y][p.x] = 0
						}
						if p.u == 2 {
							ans[p.y][p.x] = 1
						}
						if ans[p.y][p.x] != o {
							updated = true
						}
					}
					for _, p := range pp[i][j][0] {
						o := ans[p.y][p.x]
						if p.u == 3 {
							ans[p.y][p.x] = 0
						}
						if p.u == 1 {
							ans[p.y][p.x] = 1
						}
						if ans[p.y][p.x] != o {
							updated = true
						}
					}
					continue
				}
				if ans[i][j] == 0 {
					for _, p := range pp[i][j][1] {
						o := ans[p.y][p.x]
						if p.u == 1 {
							ans[p.y][p.x] = 0
						}
						if p.u == 0 {
							ans[p.y][p.x] = 1
						}
						if ans[p.y][p.x] != o {
							updated = true
						}
					}
					for _, p := range pp[i][j][0] {
						o := ans[p.y][p.x]
						if p.u == 2 {
							ans[p.y][p.x] = 0
						}
						if p.u == 0 {
							ans[p.y][p.x] = 1
						}
						if ans[p.y][p.x] != o {
							updated = true
						}
					}
				}
			}
		}
	}
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if ans[i][j] == -1 {
				ans[i][j] = 0
			}
		}
	}
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			if j > 0 {
				fmt.Fprint(writer, " ")
			}
			fmt.Fprint(writer, ans[i][j])
		}
		fmt.Fprintln(writer, "")
	}
}

func newPair(y, x, u int) pair {
	return pair{
		y: y,
		x: x,
		u: u,
	}
}

type pair struct {
	y, x, u int
}

func makeGrid(h, w int) [][][2]uint32 {
	index := make([][][2]uint32, h, h)
	data := make([][2]uint32, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}
0