Advertisement
nq1s788

26 коробки дп

May 18th, 2025 (edited)
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. data = open('26.txt').readlines()
  2. n, k, Q = map(int, data[0].split())
  3. a = []
  4. for e in data[1:]:
  5.     a.append(tuple(map(int, e.split())))
  6. a.sort()
  7. inf = 10000000
  8. mx_s = a[-1][0]
  9. dp = [[inf for j in range(Q + 1)] for i in range(mx_s + 1)]
  10. cur_a = -1
  11. mn = inf
  12. for i in range(1, mx_s + 1):
  13.     if cur_a != n - 1 and a[cur_a + 1][0] == i:
  14.         cur_a += 1
  15.     if cur_a != -1:
  16.         mn = min(mn, a[cur_a][1])
  17.         dp[i][1] = mn
  18. for q in range(2, Q + 1):
  19.     cur_a = -1
  20.     mn = inf
  21.     for i in range(1, mx_s + 1):
  22.         if cur_a != n - 1 and a[cur_a + 1][0] == i:
  23.             cur_a += 1
  24.         if a[cur_a][0] == i and i - k >= 1:
  25.             mn = min(dp[i - k][q - 1] + a[cur_a][1], mn)
  26.             dp[i][q] = mn
  27.         else:
  28.             dp[i][q] = mn
  29. print(dp[mx_s][Q])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement