0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AtCoder ABC 338 C - Leftover Recipesを整数計画問題として解く

Posted at

整数計画問題であることが自明です。料理Aを$x$人分, 料理Bを$y$人分作る時に$x+y$を最大化したいです。

maximize: $z = x + y$
subject to:$x \cdot a[i] + y \cdot b[i] \leq q[i]$

を解けば良いです。

ortools

ortools
n = int(input())
q = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))

from ortools.sat.python import cp_model
import sys
model = cp_model.CpModel()

ub = 10**6 + 10
x = model.NewIntVar(0, ub, 'x')
y = model.NewIntVar(0, ub, 'y')

for i in range(n):
    model.Add(a[i] * x + b[i] * y <= q[i])

model.Maximize(x + y)
solver = cp_model.CpSolver()
status = solver.Solve(model)

if status == cp_model.OPTIMAL:
    print(int(solver.ObjectiveValue()))
    print("x, y = ",solver.Value(x),solver.Value(y), file = sys.stderr)
else:
  assert False
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?