結果

問題 No.393 2本の竹
ユーザー aru aruaru aru
提出日時 2020-10-18 11:31:56
言語 Go
(1.22.1)
結果
MLE  
実行時間 -
コード長 2,840 bytes
コンパイル時間 11,254 ms
コンパイル使用メモリ 212,196 KB
実行使用メモリ 811,336 KB
最終ジャッジ日時 2023-09-28 08:52:29
合計ジャッジ時間 14,564 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 664 ms
267,160 KB
testcase_01 AC 11 ms
18,556 KB
testcase_02 MLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

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 route [][]int

func rec(m, n int) {
	if m == 0 {
		route = append(route, r)
		return
	}
	x := dp[m][n]
	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])
			r = r[1:]
		}
	}
	if dp[m-1][n] != -1 {
		rec(m-1, n)
	}
}

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)
	route = make([][]int, 0)
	rec(m, NN)
	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