結果

問題 No.884 Eat and Add
ユーザー ccppjsrbccppjsrb
提出日時 2020-06-04 12:47:46
言語 Go
(1.22.1)
結果
AC  
実行時間 5 ms / 1,000 ms
コード長 2,005 bytes
コンパイル時間 14,123 ms
コンパイル使用メモリ 202,008 KB
実行使用メモリ 7,628 KB
最終ジャッジ日時 2023-08-19 02:59:27
合計ジャッジ時間 10,757 ms
ジャッジサーバーID
(参考情報)
judge9 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,356 KB
testcase_01 AC 1 ms
4,356 KB
testcase_02 AC 1 ms
4,356 KB
testcase_03 AC 5 ms
7,624 KB
testcase_04 AC 5 ms
7,628 KB
testcase_05 AC 4 ms
7,624 KB
testcase_06 AC 4 ms
7,624 KB
testcase_07 AC 4 ms
7,628 KB
testcase_08 AC 4 ms
7,628 KB
testcase_09 AC 4 ms
7,624 KB
testcase_10 AC 1 ms
4,356 KB
testcase_11 AC 1 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}
func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	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 getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(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
	cnt := 0
	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 3
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}
	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	solve(scanner, writer)
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	s := getNextString(scanner)
	n := len(s)
	dp := make([][2]cint, n+1)
	for i := 0; i < n+1; i++ {
		dp[i][0] = cint(math.MaxInt32)
		dp[i][1] = cint(math.MaxInt32)
	}
	dp[n][0] = 0
	for i := n - 1; i >= 0; i-- {
		if s[i] == '1' {
			dp[i][0].min(dp[i+1][0] + 1)
			dp[i][1].min(dp[i+1][1])
			dp[i][1].min(dp[i+1][0] + 1)
			continue
		}
		dp[i][0].min(dp[i+1][0])
		dp[i][0].min(dp[i+1][1] + 1)
		dp[i][1].min(dp[i+1][1] + 1)
	}
	fmt.Fprintln(writer, *dp[0][0].min(dp[0][1] + 1))
}

type cint int

func (x *cint) min(y cint) *cint {
	if *x > y {
		*x = y
	}
	return x
}
func (x *cint) max(y cint) *cint {
	if *x < y {
		*x = y
	}
	return x
}
0