[Fortuna OJ] 4681 [GDOI2017 Mock] 8.11 Selection
Main Idea
In this problem, the upper bound on the value of any purchase plan is \(a_i \times k\), roughly on the order of \(1e5\). Consider a super brute force: let the state \(f[i][j][w]\) be whether it is feasible to have made \(i\) picks, with the last pick being coin type \(j\) and the current total being \(w\). Counting the value bound, this transition is roughly \(O(n^4)\), which only passes part of the points.
Consider transforming the state. We first subtract the minimum price from the prices of all items; then the remaining purchase plans take the form \(k*minVal + sum\). Then we let the state \(f[i][j]\) be the minimum number of picks where the last pick is item type \(i\) and the total reached is \(j\), and we just count the states whose number of picks is less than \(k\).
Code
// FOJ4681.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 550;
int n, k, dp[MAX_N * MAX_N], ai[MAX_N], mx_range, mi;
int main()
{
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &ai[i]);
sort(ai + 1, ai + 1 + n);
mi = ai[1];
for (int i = 1; i <= n; i++)
ai[i] -= mi;
mx_range = ai[n];
memset(dp, 0x3f, sizeof(dp));
dp[0] = 0;
for (int i = 1; i <= n; i++)
{
if (ai[i] == ai[i - 1])
continue;
for (int j = 1; j <= k * mx_range; j++)
if (j >= ai[i])
dp[j] = min(dp[j], dp[j - ai[i]] + 1);
}
for (int i = 0; i <= mx_range * k; i++)
if (dp[i] <= k)
printf("%d ", i + mi * k);
return 0;
}