結果

問題 No.393 2本の竹
ユーザー aru aruaru aru
提出日時 2020-10-18 11:04:51
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 2,372 bytes
コンパイル時間 9,997 ms
コンパイル使用メモリ 203,812 KB
実行使用メモリ 119,440 KB
最終ジャッジ日時 2023-09-28 08:50:01
合計ジャッジ時間 14,007 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 57 ms
80,968 KB
testcase_03 AC 44 ms
47,772 KB
testcase_04 AC 20 ms
28,900 KB
testcase_05 AC 25 ms
31,096 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 126 ms
78,980 KB
testcase_09 WA -
testcase_10 AC 78 ms
22,704 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 112 ms
58,204 KB
testcase_14 AC 80 ms
49,884 KB
testcase_15 AC 65 ms
95,536 KB
testcase_16 AC 111 ms
71,620 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 17 ms
24,800 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 112 ms
41,568 KB
testcase_24 AC 37 ms
33,584 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます

ソースコード

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

func rec(m, n int) {
	if m == 0 {
		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])
			return
		}
	}
	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)
	}
	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
			}
			dp[i][j] = max(dp[i-1][j], dp[i-1][j-a[i-1]]+1)
		}
		// out(dp[i][:N+1], dp[i][N])
	}
	r = make([]int, 0, m)
	rec(m, N)
	idx := 0
	cnt := 0
	tot := 0
	// b := make([]int, 0)
	for _, e := range a {
		if idx < len(r) && e == r[idx] {
			idx++
			continue
		}
		// b = append(b, e)
		if tot+e <= N1 {
			tot += e
			cnt++
		}
	}
	return (len(r) + cnt)
}

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