結果

問題 No.393 2本の竹
ユーザー aru aruaru aru
提出日時 2020-10-18 11:17:31
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 2,569 bytes
コンパイル時間 12,550 ms
コンパイル使用メモリ 211,064 KB
実行使用メモリ 110,188 KB
最終ジャッジ日時 2023-09-28 08:50:25
合計ジャッジ時間 16,464 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
81,120 KB
testcase_01 AC 20 ms
20,480 KB
testcase_02 AC 66 ms
80,964 KB
testcase_03 AC 68 ms
45,668 KB
testcase_04 AC 27 ms
29,028 KB
testcase_05 AC 40 ms
31,020 KB
testcase_06 AC 27 ms
22,684 KB
testcase_07 WA -
testcase_08 AC 138 ms
85,184 KB
testcase_09 AC 415 ms
109,732 KB
testcase_10 AC 95 ms
22,688 KB
testcase_11 WA -
testcase_12 AC 182 ms
68,208 KB
testcase_13 AC 150 ms
64,460 KB
testcase_14 WA -
testcase_15 AC 63 ms
95,576 KB
testcase_16 AC 119 ms
80,128 KB
testcase_17 AC 63 ms
41,484 KB
testcase_18 AC 30 ms
41,416 KB
testcase_19 AC 14 ms
20,640 KB
testcase_20 AC 28 ms
24,820 KB
testcase_21 AC 56 ms
74,836 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 51 ms
33,536 KB
testcase_25 WA -
testcase_26 AC 17 ms
22,660 KB
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)
	}
	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])
	}
	r = make([]int, 0, m)
	NN := N
	for ; NN >= 0; NN-- {
		if dp[m][NN] != -1 {
			break
		}
	}
	// out(dp[m][:N+1])
	rec(m, NN)
	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++
		}
	}
	// out(r, b)
	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