結果

問題 No.1078 I love Matrix Construction
ユーザー ccppjsrbccppjsrb
提出日時 2020-06-12 23:14:48
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 5,039 bytes
コンパイル時間 13,906 ms
コンパイル使用メモリ 222,544 KB
実行使用メモリ 32,984 KB
最終ジャッジ日時 2024-06-24 05:56:51
合計ジャッジ時間 15,289 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 14 ms
7,168 KB
testcase_03 AC 34 ms
15,232 KB
testcase_04 AC 47 ms
20,096 KB
testcase_05 WA -
testcase_06 AC 15 ms
9,560 KB
testcase_07 AC 7 ms
5,376 KB
testcase_08 AC 38 ms
17,280 KB
testcase_09 WA -
testcase_10 AC 99 ms
32,512 KB
testcase_11 AC 51 ms
23,128 KB
testcase_12 AC 83 ms
29,056 KB
testcase_13 AC 90 ms
32,984 KB
testcase_14 AC 57 ms
26,452 KB
testcase_15 AC 95 ms
29,952 KB
testcase_16 WA -
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 10 ms
5,888 KB
testcase_19 AC 25 ms
10,624 KB
testcase_20 AC 25 ms
10,624 KB
testcase_21 AC 3 ms
5,376 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