結果

問題 No.1495 パンの仕入れ
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-02-28 15:22:53
言語 Go
(1.22.1)
結果
AC  
実行時間 492 ms / 2,000 ms
コード長 3,809 bytes
コンパイル時間 17,860 ms
コンパイル使用メモリ 214,520 KB
実行使用メモリ 27,740 KB
最終ジャッジ日時 2023-10-13 21:54:22
合計ジャッジ時間 34,739 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,328 KB
testcase_01 AC 1 ms
4,332 KB
testcase_02 AC 400 ms
8,324 KB
testcase_03 AC 401 ms
6,788 KB
testcase_04 AC 400 ms
6,536 KB
testcase_05 AC 401 ms
6,616 KB
testcase_06 AC 396 ms
6,540 KB
testcase_07 AC 401 ms
6,620 KB
testcase_08 AC 403 ms
6,536 KB
testcase_09 AC 405 ms
6,592 KB
testcase_10 AC 416 ms
6,544 KB
testcase_11 AC 401 ms
6,624 KB
testcase_12 AC 194 ms
6,328 KB
testcase_13 AC 192 ms
6,320 KB
testcase_14 AC 192 ms
6,360 KB
testcase_15 AC 193 ms
6,324 KB
testcase_16 AC 192 ms
6,328 KB
testcase_17 AC 192 ms
6,324 KB
testcase_18 AC 192 ms
6,392 KB
testcase_19 AC 193 ms
6,340 KB
testcase_20 AC 192 ms
6,336 KB
testcase_21 AC 191 ms
6,364 KB
testcase_22 AC 479 ms
26,580 KB
testcase_23 AC 492 ms
27,740 KB
testcase_24 AC 163 ms
5,996 KB
testcase_25 AC 162 ms
5,720 KB
testcase_26 AC 160 ms
5,832 KB
testcase_27 AC 160 ms
6,016 KB
testcase_28 AC 468 ms
24,796 KB
testcase_29 AC 472 ms
25,088 KB
testcase_30 AC 151 ms
6,568 KB
testcase_31 AC 151 ms
6,028 KB
testcase_32 AC 151 ms
6,292 KB
testcase_33 AC 152 ms
6,324 KB
testcase_34 AC 152 ms
5,916 KB
testcase_35 AC 190 ms
6,352 KB
testcase_36 AC 190 ms
6,352 KB
testcase_37 AC 190 ms
6,372 KB
testcase_38 AC 190 ms
6,324 KB
testcase_39 AC 193 ms
6,356 KB
testcase_40 AC 147 ms
5,944 KB
testcase_41 AC 146 ms
6,020 KB
testcase_42 AC 146 ms
6,296 KB
testcase_43 AC 152 ms
6,336 KB
testcase_44 AC 146 ms
6,044 KB
testcase_45 AC 1 ms
4,372 KB
testcase_46 AC 3 ms
4,372 KB
testcase_47 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var N, M, K int
		fmt.Fscan(in, &N, &M, &K)
		// 这里的函数 f[i] = sum(Bxi-yi)^2 (1<=i<=M)
		A, B, C := make([]int, N), make([]int, N), make([]int, N)
		for i := 0; i < M; i++ {
			var x, y int
			fmt.Fscan(in, &x, &y)
			x--
			A[x]++
			B[x] -= y * 2
			C[x] += y * y
		}
		funcs := make([]SlopeFunc, N)
		for i := 0; i < N; i++ {
			funcs[i] = NewQuadratic(A[i], B[i], C[i], 0, K)
		}
		coeff := make([]int, N)
		for i := 0; i < N; i++ {
			coeff[i] = 1
		}
		res, _, _ := MinConvexSumUnderLinearConstraint(coeff, funcs, K)
		fmt.Fprintln(out, res)
	}
}

const INF int = 1e18

// !minimize sum(fi(xij) for j in range(1, ki+1) for i in range(1, n+1))
//  k: coefficient of each variable
//  f: convex function
//  c: constraint (sum of all variables)
//  return: (y, [[(x_i, # of such x_i), ... ], ...])
func MinConvexSumUnderLinearConstraint(k []int, f []SlopeFunc, c int) (minimum int, res [][][2]int, ok bool) {
	if len(k) != len(f) {
		panic("len(k) != len(f)")
	}
	lowerSum, upperSum := 0, 0
	for _, func_ := range f {
		lowerSum += func_.getLower()
		upperSum += func_.getUpper()
	}
	if lowerSum > c || upperSum < c {
		return
	}

	n := len(k)
	few, enough := -INF, INF
	for enough-few > 1 {
		slope := few + (enough-few)/2
		cnt := 0
		for i := 0; i < n; i++ {
			tmp := f[i].Slope(slope)
			cnt += tmp * k[i]
			if cnt >= c {
				break
			}
		}
		if cnt >= c {
			enough = slope
		} else {
			few = slope
		}
	}

	res = make([][][2]int, n)
	additional := []int{}
	ctmp := 0
	for i := 0; i < n; i++ {
		xLower := f[i].Slope(few)
		xUpper := f[i].Slope(few + 1)
		ctmp += k[i] * xLower
		res[i] = append(res[i], [2]int{xLower, k[i]})
		if xLower < xUpper {
			additional = append(additional, i)
		}
		minimum += k[i] * f[i].Eval(xLower)
	}

	minimum += (c - ctmp) * (few + 1)
	for len(additional) > 0 {
		i := additional[len(additional)-1]
		additional = additional[:len(additional)-1]
		add := 0
		if c-ctmp > k[i] {
			add = k[i]
		} else {
			add = c - ctmp
		}
		x := res[i][0][0]
		if add != 0 {
			res[i][0][1] -= add
			if res[i][0][1] == 0 {
				res[i] = res[i][:len(res[i])-1]
			}
			res[i] = append(res[i], [2]int{x + 1, add})
			ctmp += add
		}
	}

	ok = true
	return
}

type SlopeFunc interface {
	Slope(s int) int
	Eval(x int) int
	getLower() int
	getUpper() int
}

// ax^2 + bx + c (convex), lower <= x <= upper
type Quadratic struct{ a, b, c, lower, upper int }

func NewQuadratic(a, b, c, lower, upper int) *Quadratic { return &Quadratic{a, b, c, lower, upper} }

func (q *Quadratic) Slope(s int) int {
	if q.a == 0 {
		if q.b <= s {
			return q.upper
		}
		return q.lower
	}
	res := (s + q.a - q.b) / (q.a * 2)
	if res > q.upper {
		return q.upper
	}
	if res < q.lower {
		return q.lower
	}
	return res
}

func (q *Quadratic) Eval(x int) int { return (q.a*x+q.b)*x + q.c }

// f(x) - f(x - 1)
func (q *Quadratic) nextCost(x int) int { return 2*q.a*x - q.a + q.b }
func (q *Quadratic) getLower() int      { return q.lower }
func (q *Quadratic) getUpper() int      { return q.upper }

// x^3 - ax, x >= 0 (convex)
type Cubic struct {
	a, lower, upper int
}

func NewCubic(a, upper int) *Cubic { return &Cubic{a, 0, upper} }
func (c *Cubic) Slope(s int) int {
	lo, hi := c.lower, c.upper+1
	for hi-lo > 1 {
		mid := (lo + hi) / 2
		if c.nextCost(mid) <= s {
			lo = mid
		} else {
			hi = mid
		}
	}
	return lo
}

func (c *Cubic) Eval(x int) int { return (x*x - c.a) * x }

// f(x) - f(x - 1)
func (c *Cubic) nextCost(x int) int { return 3*x*x - 3*x + 1 - c.a }
func (q *Cubic) getLower() int      { return q.lower }
func (q *Cubic) getUpper() int      { return q.upper }
0