結果

問題 No.393 2本の竹
ユーザー aru aruaru aru
提出日時 2020-10-18 11:39:22
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 3,392 bytes
コンパイル時間 10,106 ms
コンパイル使用メモリ 205,688 KB
実行使用メモリ 210,436 KB
最終ジャッジ日時 2023-09-28 08:53:06
合計ジャッジ時間 14,418 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
210,436 KB
testcase_01 AC 9 ms
18,560 KB
testcase_02 AC 31 ms
75,300 KB
testcase_03 AC 32 ms
41,436 KB
testcase_04 AC 14 ms
35,144 KB
testcase_05 AC 15 ms
26,956 KB
testcase_06 AC 10 ms
20,672 KB
testcase_07 AC 27 ms
41,532 KB
testcase_08 AC 67 ms
74,944 KB
testcase_09 AC 327 ms
130,428 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 74 ms
41,488 KB
testcase_13 AC 58 ms
56,136 KB
testcase_14 WA -
testcase_15 AC 28 ms
56,608 KB
testcase_16 WA -
testcase_17 AC 25 ms
47,748 KB
testcase_18 AC 14 ms
31,080 KB
testcase_19 AC 6 ms
14,388 KB
testcase_20 AC 11 ms
22,744 KB
testcase_21 AC 25 ms
51,948 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 19 ms
27,196 KB
testcase_25 WA -
testcase_26 AC 9 ms
16,480 KB
testcase_27 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

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

func out(x ...interface{}) {
	fmt.Fprintln(wr, x...)
}

func getI() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getF() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int) []int {
	ret := make([]int, N)
	for i := 0; i < N; i++ {
		ret[i] = getI()
	}
	return ret
}

func getS() string {
	sc.Scan()
	return sc.Text()
}

// min, max, asub, absなど基本関数
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 asub(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

const inf = 99

var a []int
var m, n int
var dp [][]int
var r []int
var result int

func calc(r []int, N1 int) {
	total := 0
	for _, v := range r {
		total += v
	}
	// fmt.Fprint(wr, r, total, " ")
	idx := 0
	cnt := 0
	tot := 0
	b := make([]int, 0)
	for _, e := range a {
		if idx < len(r) && e == r[idx] {
			idx++
			continue
		}
		if tot+e <= N1 {
			tot += e
			cnt++
			b = append(b, e)
		}
	}
	// out(b, tot)
	result = max(result, len(r)+cnt)
}

func rec(m, n, N1, prev int) {
	if m == 0 {
		calc(r, N1)
		return
	}
	x := dp[m][n]
	flg := false
	if n-a[m-1] >= 0 {
		y := dp[m-1][n-a[m-1]]
		if y+1 == x {
			r = append([]int{a[m-1]}, r...)
			rec(m-1, n-a[m-1], N1, a[m-1])
			r = r[1:]
			flg = true
		}
	}
	if dp[m-1][n] != -1 {
		if prev == a[m-1] && flg == true {

		} else {
			rec(m-1, n, N1, a[m-1])
		}
	}
}

func g(N, N1 int) int {
	n = m + 1
	dp = make([][]int, n)
	for i := 0; i < n; i++ {
		dp[i] = make([]int, 110000)
	}
	for i := 0; i <= N; i++ {
		dp[0][i] = -1
	}
	dp[0][0] = 0
	for i := 1; i < n; i++ {
		for j := 0; j <= N; j++ {
			dp[i][j] = dp[i-1][j]
			if j-a[i-1] < 0 {
				continue
			}
			if dp[i-1][j-a[i-1]] == -1 {
				continue
			}
			dp[i][j] = max(dp[i-1][j], dp[i-1][j-a[i-1]]+1)
		}
		// out(dp[i][:N+1], dp[i][N])
	}
	NN := N
	for ; NN >= 0; NN-- {
		if dp[m][NN] != -1 {
			break
		}
	}
	// out(dp[m][:N+1])
	r = make([]int, 0, m)
	result = 0
	rec(m, NN, N1, -1)
	ret := result
	// ret := 0
	// for _, r := range route {
	// 	total := 0
	// 	for _, v := range r {
	// 		total += v
	// 	}
	// 	// fmt.Fprint(wr, r, total, " ")
	// 	idx := 0
	// 	cnt := 0
	// 	tot := 0
	// 	b := make([]int, 0)
	// 	for _, e := range a {
	// 		if idx < len(r) && e == r[idx] {
	// 			idx++
	// 			continue
	// 		}
	// 		if tot+e <= N1 {
	// 			tot += e
	// 			cnt++
	// 			b = append(b, e)
	// 		}
	// 	}
	// 	// out(b, tot)
	// 	ret = max(ret, len(r)+cnt)
	// }
	return ret
}

func f() {
	n1, n2 := getI(), getI()
	m = getI()
	a = getInts(m)
	sort.Ints(a)
	ret := g(n1, n2)
	//	ret = max(g(n2, n1), ret)
	out(ret)
}

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, 1000000)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	d := getI()
	for i := 0; i < d; i++ {
		f()
	}
}
0