結果

問題 No.2276 I Want AC
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-08-22 12:42:20
言語 Go
(1.22.1)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 3,131 bytes
コンパイル時間 14,712 ms
コンパイル使用メモリ 209,676 KB
実行使用メモリ 10,156 KB
最終ジャッジ日時 2023-08-22 12:42:39
合計ジャッジ時間 16,321 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 5 ms
4,380 KB
testcase_12 AC 13 ms
7,716 KB
testcase_13 AC 5 ms
4,376 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 8 ms
5,636 KB
testcase_16 AC 7 ms
5,632 KB
testcase_17 AC 13 ms
7,864 KB
testcase_18 AC 9 ms
5,648 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 16 ms
7,888 KB
testcase_21 AC 5 ms
5,628 KB
testcase_22 AC 3 ms
4,376 KB
testcase_23 AC 14 ms
7,864 KB
testcase_24 AC 15 ms
7,860 KB
testcase_25 AC 14 ms
7,864 KB
testcase_26 AC 16 ms
7,880 KB
testcase_27 AC 16 ms
7,864 KB
testcase_28 AC 13 ms
7,708 KB
testcase_29 AC 11 ms
5,644 KB
testcase_30 AC 15 ms
7,864 KB
testcase_31 AC 10 ms
5,628 KB
testcase_32 AC 15 ms
7,864 KB
testcase_33 AC 15 ms
7,860 KB
testcase_34 AC 16 ms
7,880 KB
testcase_35 AC 7 ms
5,624 KB
testcase_36 AC 6 ms
5,624 KB
testcase_37 AC 19 ms
10,156 KB
testcase_38 AC 8 ms
5,628 KB
testcase_39 AC 7 ms
5,624 KB
testcase_40 AC 8 ms
5,624 KB
testcase_41 AC 7 ms
5,628 KB
testcase_42 AC 18 ms
10,096 KB
testcase_43 AC 19 ms
10,096 KB
testcase_44 AC 6 ms
5,628 KB
testcase_45 AC 15 ms
7,864 KB
testcase_46 AC 16 ms
7,864 KB
testcase_47 AC 8 ms
5,624 KB
testcase_48 AC 7 ms
5,624 KB
testcase_49 AC 16 ms
7,864 KB
testcase_50 AC 16 ms
7,864 KB
testcase_51 AC 16 ms
7,868 KB
testcase_52 AC 16 ms
7,864 KB
testcase_53 AC 16 ms
7,868 KB
testcase_54 AC 13 ms
5,648 KB
testcase_55 AC 11 ms
5,636 KB
testcase_56 AC 10 ms
5,632 KB
testcase_57 AC 10 ms
5,628 KB
testcase_58 AC 11 ms
5,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

func main() {
	yuki2276()
}

func demo() {
	fmt.Println(FibonacciSearch(func(x int) int { return x * x }, 0, 100, true))
	fmt.Println(FibonacciSearch(func(x int) int { return x * x }, 0, 100, false))
}

// 2448. 使数组相等的最小开销
// https://leetcode.cn/problems/minimum-cost-to-make-array-equal/
func minCost(nums []int, cost []int) int64 {
	abs := func(x int) int {
		if x < 0 {
			return -x
		}
		return x
	}

	_, y := (FibonacciSearch(func(pos int) int {
		sum := 0
		for i, num := range nums {
			sum += abs(num-pos) * cost[i]
		}
		return sum
	}, 0, 1e6, true))

	return int64(y)
}

func yuki2276() {
	// 替换问号序列后得到AC子序列的最大方案数
	// https://yukicoder.me/problems/no/2276
	//
	// A, C, ? からなる長さ N の文字列 S が与えられます。
	// あなたは、S に対して次の操作を行います。
	// !S に含まれる ? を、それぞれ A または C のいずれかで書き換える。
	// !操作後、S から(連続するとは限らない)部分列として AC を取り出す方法の数が得点となります。
	// ここで、部分列を取り出す方法が異なるとは、取り出す位置が異なることをいいます。
	// 得点の最大値を求めてください。

	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n int
	fmt.Fscan(in, &n)
	var s string
	fmt.Fscan(in, &s)

	sb := []byte(s)

	replacePos := make([]int, 0)
	for i := 0; i < n; i++ {
		if s[i] == '?' {
			replacePos = append(replacePos, i)
		}
	}

	// 将前n个?替换为A,后n个?替换为C,求AC的个数
	fun := func(n int) int {
		if !(0 <= n && n <= len(replacePos)) {
			panic("n out of range")
		}

		for i := 0; i < n; i++ {
			sb[replacePos[i]] = 'A'
		}
		for i := n; i < len(replacePos); i++ {
			sb[replacePos[i]] = 'C'
		}

		countA := 0
		countAC := 0
		for _, v := range sb {
			if v == 'A' {
				countA++
			}
			if v == 'C' {
				countAC += countA
			}
		}

		return countAC
	}

	_, y := FibonacciSearch(fun, 0, len(replacePos), false)
	fmt.Fprintln(out, y)
}

const INF int = 1e18

// 寻找[left,right]中的一个极值点,不要求单峰性质.
//
//	返回值: (极值点,极值)
func FibonacciSearch(f func(x int) int, left, right int, minimize bool) (int, int) {
	a, b, c, d := left, left+1, left+2, left+3
	step := 0
	for d < right {
		b = c
		c = d
		d = b + c - a
		step++
	}

	get := func(i int) int {
		if right < i {
			return INF
		}
		if minimize {
			return f(i)
		}
		return -f(i)
	}

	ya, yb, yc, yd := get(a), get(b), get(c), get(d)
	for i := 0; i < step; i++ {
		if yb < yc {
			d = c
			c = b
			b = a + d - c
			yd = yc
			yc = yb
			yb = get(b)
		} else {
			a = b
			b = c
			c = a + d - b
			ya = yb
			yb = yc
			yc = get(c)
		}
	}

	x := a
	y := ya
	if yb < y {
		x = b
		y = yb
	}
	if yc < y {
		x = c
		y = yc
	}
	if yd < y {
		x = d
		y = yd
	}

	if minimize {
		return x, y
	}
	return x, -y
}

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

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