結果

問題 No.1765 While Shining
ユーザー HimaHima
提出日時 2022-06-18 11:29:51
言語 Go
(1.22.1)
結果
AC  
実行時間 13 ms / 2,000 ms
コード長 905 bytes
コンパイル時間 13,652 ms
コンパイル使用メモリ 219,236 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-18 05:17:56
合計ジャッジ時間 15,191 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 2 ms
6,812 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 8 ms
6,940 KB
testcase_10 AC 11 ms
6,940 KB
testcase_11 AC 10 ms
6,940 KB
testcase_12 AC 11 ms
6,944 KB
testcase_13 AC 10 ms
6,940 KB
testcase_14 AC 12 ms
6,940 KB
testcase_15 AC 13 ms
6,940 KB
testcase_16 AC 12 ms
6,944 KB
testcase_17 AC 13 ms
6,940 KB
testcase_18 AC 12 ms
6,940 KB
testcase_19 AC 13 ms
6,944 KB
testcase_20 AC 12 ms
6,944 KB
testcase_21 AC 13 ms
6,940 KB
testcase_22 AC 13 ms
6,940 KB
testcase_23 AC 13 ms
6,940 KB
testcase_24 AC 10 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

var sc = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

func solve(n int, a []int) int {
	if n == 1 {
		return 0
	}
	s := make([]int, n)
	for i := n - 2; i >= 0; i-- {
		if a[i] != a[i+1] {
			s[i] = s[i+1] + 1
		}
	}
	//fmt.Println(a)
	//fmt.Println(s)

	var ans int
	for i := 0; i < n-1; i++ {
		if a[i] == 1 {
			ans += s[i]
			if n-i-1 > s[i] {
				ans++
			}
		}
	}
	return ans
}
func main() {
	buf := make([]byte, 1024*1024)
	sc.Buffer(buf, bufio.MaxScanTokenSize)
	sc.Split(bufio.ScanWords)

	n := nextInt()
	a := nextIntSlice(n)
	ans := solve(n, a)
	PrintInt(ans)
}

func nextInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}

func nextIntSlice(n int) []int {
	s := make([]int, n)
	for i := range s {
		s[i] = nextInt()
	}
	return s
}

func PrintInt(x int) {
	defer out.Flush()
	fmt.Fprintln(out, x)
}
0