Submission #62286414


Source Code Expand

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	solve(os.Stdin, os.Stdout)
}

func solve(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()

	n := readNum(in)
	s := readString(in)
	type pair struct{ x, y int }
	dp := make([][]pair, n+1)
	dp[0] = make([]pair, len(s))
	for i := 0; i < len(s); i++ {
		if s[i] == '1' {
			dp[0][i] = pair{1, 0}
		} else {
			dp[0][i] = pair{0, 1}
		}
	}

	for i := 1; i <= n; i++ {
		tmp := []pair{}
		for j := 0; j < len(dp[i-1]); j += 3 {
			tmp1 := min(dp[i-1][j].x+dp[i-1][j+1].x+dp[i-1][j+2].x, min(dp[i-1][j].x+dp[i-1][j+1].x+dp[i-1][j+2].y, min(dp[i-1][j].x+dp[i-1][j+1].y+dp[i-1][j+2].x, dp[i-1][j].y+dp[i-1][j+1].x+dp[i-1][j+2].x)))
			tmp2 := min(dp[i-1][j].y+dp[i-1][j+1].y+dp[i-1][j+2].y, min(dp[i-1][j].y+dp[i-1][j+1].y+dp[i-1][j+2].x, min(dp[i-1][j].y+dp[i-1][j+1].x+dp[i-1][j+2].y, dp[i-1][j].x+dp[i-1][j+1].y+dp[i-1][j+2].y)))
			tmp = append(tmp, pair{tmp1, tmp2})
		}
		dp[i] = tmp
	}
	fmt.Fprintln(out, dp[n][0].x+dp[n][0].y)
}

func max(a, b int) int {
	if a >= b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a <= b {
		return a
	}
	return b
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' || s[i] == '\r' {
			return s[:i]
		}
	}
	return s
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

Submission Info

Submission Time
Task E - Hierarchical Majority Vote
User xylu
Language Go (go 1.20.6)
Score 450
Code Size 2375 Byte
Status AC
Exec Time 47 ms
Memory 65668 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 2
AC × 32
Set Name Test Cases
Sample 00_sample_01.txt, 00_sample_02.txt
All 00_sample_01.txt, 00_sample_02.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 02_handmade_05.txt, 02_handmade_06.txt, 02_handmade_07.txt, 02_handmade_08.txt, 02_handmade_09.txt, 02_handmade_10.txt
Case Name Status Exec Time Memory
00_sample_01.txt AC 1 ms 1632 KiB
00_sample_02.txt AC 1 ms 1628 KiB
01_random_01.txt AC 1 ms 2548 KiB
01_random_02.txt AC 47 ms 65656 KiB
01_random_03.txt AC 1 ms 1628 KiB
01_random_04.txt AC 47 ms 65656 KiB
01_random_05.txt AC 2 ms 4844 KiB
01_random_06.txt AC 44 ms 65652 KiB
01_random_07.txt AC 5 ms 9164 KiB
01_random_08.txt AC 47 ms 65656 KiB
01_random_09.txt AC 1 ms 1632 KiB
01_random_10.txt AC 46 ms 65652 KiB
01_random_11.txt AC 1 ms 1652 KiB
01_random_12.txt AC 41 ms 65656 KiB
01_random_13.txt AC 0 ms 1624 KiB
01_random_14.txt AC 47 ms 65652 KiB
01_random_15.txt AC 1 ms 1940 KiB
01_random_16.txt AC 46 ms 64828 KiB
01_random_17.txt AC 1 ms 2548 KiB
01_random_18.txt AC 47 ms 64836 KiB
01_random_19.txt AC 1 ms 1668 KiB
01_random_20.txt AC 47 ms 64832 KiB
02_handmade_01.txt AC 41 ms 65656 KiB
02_handmade_02.txt AC 41 ms 65288 KiB
02_handmade_03.txt AC 41 ms 65668 KiB
02_handmade_04.txt AC 41 ms 65288 KiB
02_handmade_05.txt AC 41 ms 65288 KiB
02_handmade_06.txt AC 42 ms 65288 KiB
02_handmade_07.txt AC 42 ms 65660 KiB
02_handmade_08.txt AC 42 ms 65656 KiB
02_handmade_09.txt AC 42 ms 65660 KiB
02_handmade_10.txt AC 42 ms 65660 KiB